Im trying to get a 3 button password sequence working I'm trying get a password system working that when you click 3 buttons in a certain order (1, 2, 3, 3, 1) then an LED will light up. But as I'm going to implement this into a larger code I can't just have the void loop() going forever to wait for it.

I think my issue is in the while loops and making it wait for one of the buttons to be pressed but I don't know how to get it to work.

thanks 

#define greenLED A0
#define redLED A1

int button1 = 5;
int button2 = 4;
int button3 = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  int total = 0;
  while(digitalRead(button1) == LOW &amp;&amp; digitalRead(button2) == LOW &amp;&amp; digitalRead(button3) == LOW){
    while(digitalRead(button1) == LOW &amp;&amp; digitalRead(button2) == LOW &amp;&amp; digitalRead(button3) == LOW){
      while(digitalRead(button1) == LOW &amp;&amp; digitalRead(button2) == LOW &amp;&amp; digitalRead(button3) == LOW){
        while(digitalRead(button1) == LOW &amp;&amp; digitalRead(button2) == LOW &amp;&amp; digitalRead(button3) == LOW){
          while(digitalRead(button1) == LOW &amp;&amp; digitalRead(button2) == LOW &amp;&amp; digitalRead(button3) == LOW){
            if(digitalRead(button1) == HIGH){
              total = total + 1;
            }
          }
          if(digitalRead(button2)==HIGH){
            total = total+1;
          }
        } 
        if(digitalRead(button3)==HIGH){
          total = total + 1;
        }
      }
      if(digitalRead(button3)==HIGH){
        total = total+1;
      }
    }
    if(digitalRead(button1) == HIGH){
      total = total+1;
    }
  }
  if(total == 5){
    digitalWrite(A0, HIGH);
    delay(2000);
    digitalWrite(A0, LOW);
  }else{
    digitalWrite(A1, HIGH);
    delay(2000);
    digitalWrite(A1, LOW);
  }
}
Without programming everything out, use something like this:


Initialization:


Create a (global) variable of string ("12331") which is the code
Create a (global) variable of int that shows the number of correct numbers so far (initially 0)

In the while loop:


Check which button is pressed
If it is the correct button
increase the int variable
If that variable is 5 (meaning all numbers ok), light up the LED
Delay
Make the variable 0 again 
Clear the LED
If the wrong button is pressed
If it is equal to the first value ('1')


Set the variable to 1 (since the wrong number is the first correct number)

Else


Set the variable to 0