Combination Lock

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 :stuck_out_tongue_closed_eyes:

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.

You need to store the correct sequence of buttons in an array, and have an "index" variable going along it (or returning to the start) depending on the detected button pressed. Also, you'll have to use the digitalRead() function to actually read inputs...

Here's a very messy sketch:

if(SwitchState1 > prevSwitchState1)
     SwitchState3 > prevSwitchState3
     SwitchState2 > prevSwitchState2){

No, a sketch would compile.

Think about how a combination lock works.
It goes from state to to state - first digit correct/incorrect, second digit correct/incorrect etc.

This will compile

(prevSwitchState1 != SwitchState1;)
       (prevSwitchState2 != SwitchState2;)
       (prevSwitchState3 != SwitchState3;)

but it will not do anything useful.
What do you think it does?

igendel:
You need to store the correct sequence of buttons in an array, and have an "index" variable going along it (or returning to the start) depending on the detected button pressed. Also, you'll have to use the digitalRead() function to actually read inputs...

Okay, thanks for the response. I was thinking about using an array, but I didn't think that would work. Thank you!