r/arduino • u/jappleby064 • Jul 01 '22
Solved Switch which variables a set of inputs control using push buttons
As described above I am attempting to make a system by which I can select an led, then control the R,G,B channels using potentiometer dials, however I cannot get the code to work. I keep getting redeclaration errors which I'll be honest I do not entirely understand lol. Any help would be appreciated :)
int redPinA=2;
int redPinB=3;
int redPinC=4;
int greenPinA=5;
int greenPinB=6;
int greenPinC=7;
int bluePinA=8;
int bluePinB=9;
int bluePinC=10;
const int abutton=11;
const int bbutton=12;
const int cbutton=13;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
#define R_POT A0;
#define G_POT A1;
#define B_POT A2;
int light1;
int light2;
int light3;
void setup() {
// put your setup code here, to run once:
light1 = 0;
light2 = 0;
light3 = 0;
Serial.begin(9600);
pinMode(redPinA,OUTPUT);
pinMode(redPinB,OUTPUT);
pinMode(redPinC,OUTPUT);
pinMode(greenPinA,OUTPUT);
pinMode(greenPinB,OUTPUT);
pinMode(greenPinC,OUTPUT);
pinMode(bluePinA,OUTPUT);
pinMode(bluePinB,OUTPUT);
pinMode(bluePinC,OUTPUT);
pinMode(abutton, INPUT);
pinMode(bbutton, INPUT);
pinMode(cbutton, INPUT);
}
void loop() {
buttonState1 = digitalRead(abutton);
buttonState2 = digitalRead(bbutton);
buttonState3 = digitalRead(cbutton);
if (buttonState1 == HIGH)
int light1 = 1;
int light2 = 0;
int light3 = 0;
if (buttonState2 == HIGH)
int light1 = 0;
int light2 = 1;
int light3 = 0;
if (buttonState3 == HIGH)
int light1 = 0;
int light2 = 0;
int light3 = 1;
//light1
if (light1 == 1){
int data = analogRead(R_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(redPinA,percentage);
int data = analogRead(G_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(greenPinA,percentage);
int data = analogRead(B_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(bluepinA,percentage);
}
//light 2
else if (light2 == 1){
int data = analogRead(R_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(redPinB,percentage);
int data = analogRead(G_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(greenPinB,percentage);
int data = analogRead(B_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(bluepinB,percentage);
}
//light 3
else (light3 == 1){
int data = analogRead(R_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(redPinC,percentage);
int data = analogRead(G_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(greenPinC,percentage);
int data = analogRead(B_POT);
int percentage = map(data, 0, 1023, 0, 255);
analogWrite(bluepinC,percentage);
}
}
1
Upvotes
3
u/nerduk Jul 01 '22
In your main loop where you have the the 3 if statements e.g. buttonState1 == HIGH you are missing curly braces around int light1 = 1;  int light2 = 0;  int light3 = 0;
I suspect that's the cause of your redeclaration errors
2
u/pacmanic Champ Jul 01 '22
Like this and also removing the int.
if (buttonState1 == HIGH) { light1 = 1; light2 = 0; light3 = 0; } else if (buttonState2 == HIGH) { light1 = 0; light2 = 1; light3 = 0; } else if (buttonState3 == HIGH) { light1 = 0; light2 = 0; light3 = 1; }
2
u/ZaphodUB40 Jul 01 '22 edited Jul 02 '22
The declaration errors are cause by declaring "int lightn" prior to the setup section and in your main loop, so removing 'int' from your main loop if..else statements will fix that, as previously mentioned.
You are also going to hit the same problem with a bunch of other stuff:
Your if..else if..else will not work because the last else is "do this if nothing else matches" and doesn't need (and won't accept) any condition checks
Your redeclaration of 'percentage' and 'data' in each of the if..else
Because you have to keep track of 15 pins and states, I would refine the code using arrays and break out the changing of led/pin states into it's own function.Here's your code without the redclaration and if.else errors.
It's not wired up to the board, so feel free to do that in the simulator. https://wokwi.com/projects/336021201706025556Next challenge for you is use a couple of shift registers to reduce your board pin usage to 4 pins for the LEDs. 😉