Pause code until button is pressed.

After browsing through some forums, I found out that the simplest way to do this is with a while loop.

I am running the buttons using the built in pullup resistor so the the button is not pressed when HIGH and pressed when LOW. From what I understand, when the while loop condition is not true(so the button is pressed) it should end the loop and continue with the rest of the code. This isn't happening for me.

int bluebutton = 11;
int redbutton = 12;

void setup() {
  // put your setup code here, to run once:


pinMode(redbutton, INPUT_PULLUP);
pinMode(bluebutton, INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly


while(digitalRead(bluebutton == HIGH or redbutton == HIGH)){


lcd.print("Which mode would you");
lcd.print("   like to play?");
delay(4000);
lcd.clear();
lcd.print("Press red for singleplayer and blue for 2-player.");
delay(3000);


}

//When the while loops condition is not true, the loop should break and continue with the rest of the code.


}
digitalRead(bluebutton == HIGH or redbutton == HIGH)

Check that digitalRead() carefully. Which pin is it reading ?

It's reading digital pins 11 and 12.

keshmaster81:
It’s reading digital pins 11 and 12.

Try again

digitalRead() can only read one pin at a time. Check the syntax

digitalRead(pinNumber);

I changed it to this and it still doesn't work.

while(digitalRead(redbutton == HIGH)){

}

I also tried this but I get an error saying redbuttoninput was not declared in this scope.

void loop() {

redbuttoninput = digitalRead(redbutton);

while (redbuttoninput == HIGH)){

}

}

Put an int in front of redbutton

See the following FAQ:

My example

digitalRead(pinNumber);

Your code

while(digitalRead(redbutton == HIGH)){

spot the difference

redbuttoninput = digitalRead(redbutton);
while (redbuttoninput == HIGH)){

Where is redbuttoninput declared ?