Nested if statement

I am trying to make a code when 3 buttons are pressed in a sequence (3,1,2) a led lights up. I have tried a nested if statement but it doesn’t seem to work.

Hi Photon(great username, btw) -
Can you share the code you’re using? How are you storing the state of the buttons?

Sorry it took so long I forgot I wrote this it here is my code:

int button1 = D0;
int button2 = D1;
int button3 = D2;
int led1 = D3;
void setup(){
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);  
void loop() {
  while (digitalRead(button3 = HIGH)) {
    digitalWrite(led1, LOW);
  if (digitalRead(button3 = LOW)) {
    if (digitalRead(button1 = LOW)) {  
      if (digitalRead(button2 = LOW)) {
        digitalWrite(led1, HIGH);
  else {
    digitalWrite(led1, LOW);

With that nested if construct you’d need to either already hold button 1 and 2 down when you press button 3 or you’d need to be lightning fast to press them in sequence. You’d only have a few microseconds to do that, which is impossible.

I’d suggest a FSM (finite state machine) to do what you want.

BTW, if you haven’t got external pull-ups on your buttons I’d suggest you use INPUT_PULLUP instead of INPUT to avoid floating pins when the buttons aren’t pressed.

Also your while() loop is a bad idea as it would break the cloud connection keeping the code flow trapped and not allowing for the background tasks - which would execute between iterations of loop() - to execute.


Ok thank you. I will try out your tips and let you know if I have any more questions!


Good luck!