Hello,
I am trying to make a project in which pressing certain push buttons in a certain order make a green LED go on, and if the combination is wrong, a red LED go off. Is there a way for the Arduino to detect a combination of buttons pressed? I tried if/else statements, but I haven't got them to work. I'm a noob at this, so don't grill me too hard ![]()
Here's a very messy sketch:
const int button1 = 11;
const int button2 = 10;
const int button3 = 9;
const int redlight = 2;
const int greenlight = 3;
int SwitchState1 = 0;
int SwitchState2 = 0;
int SwitchState3 = 0;
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
int prevSwitchState3 = 0;
void setup() {
pinMode(redlight, OUTPUT);
pinMode(greenlight, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
digitalWrite(redlight, LOW);
digitalWrite(greenlight, LOW);
delay(50);
}
void loop() {
if(SwitchState1 > prevSwitchState1)
SwitchState3 > prevSwitchState3
SwitchState2 > prevSwitchState2){
digitalWrite(greenlight, HIGH);
digitalWrite(redlight, LOW);
(prevSwitchState1 != SwitchState1;)
(prevSwitchState2 != SwitchState2;)
(prevSwitchState3 != SwitchState3;)
}
else{
digitalWrite(redlight, HIGH);
digitalWrite(greenlight, LOW);
}
}
Any help would be greatly appreciated. Thank you!
Note: I do not have a picture at the moment, but I have three buttons and to LEDs hooked up to their respective pins.