[SOLVED] While loop with two expressions

In the code that follows, I want the while loop to do nothing, as long as the read_LCD_buttons() returns 1, 2, 4 or 5. And to move on if button 0 or 3 is pressed How do I do this? Using the 0 || 3, only makes the button mentioned first work. Can I have two expressions in any way? I have an lcd shield with 5 pushbuttons, attached to an uno. 0, 1, 2, 3 and 4 are physical buttons, 5 is when no button is pushed (And I also want to stay in the while loop doing nothing, when no button is pushed.) Let me know if you need more of the code to help me out.

while (read_LCD_buttons() != 0 || 3){} //Do nothing
   
   if (read_LCD_buttons() == 0) {          //If button 0 is presses, do this
      lcd.print("Vanilje valgt   ");
      RightInput = 1;
      delay(2000);
     }
  
  if (read_LCD_buttons() == 3){             //If button 3 is presses, do this
      lcd.print("Kakao valgt     ");
      LeftInput = 1;
      delay(2000);
    }

Probably the simplest:

int state;
while (true)
  {
  state = read_LCD_buttons();
  if (state == 0 || state == 3) 
    break;  // time to move on
  }  // end of while loop

switch (state)
  {
  case 0:
    // handle 0 button
    break;

  case 3:
    // handle 3 button
    break;

  } // end of switch

However this is "blocking" code - you won't do anything else until the correct button is pressed.

Another approach:

while (true)
  {
  state = read_LCD_buttons();
  if (state == 0)
    {
    // handle button 0
    break;  // leave loop
    }
  else if (state == 3)
    {
    // handle button 3
    break;  // leave loop
    }
  }  // end of while loop

loop() is already called in a loop, usually you only need that. Seen blinkWithoutDelay?

This worked great for the job. Thank you :slight_smile: