Pause Code Until Button Is Pressed. - Arduino Forum

Pause code until button is pressed. Projects Programming September 3, 2020, 9:40pm 1

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. } September 3, 2020, 9:43pm 2 digitalRead(bluebutton == HIGH or redbutton == HIGH)

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

September 3, 2020, 9:45pm 3

It's reading digital pins 11 and 12.

September 3, 2020, 9:47pm 4

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); September 3, 2020, 9:54pm 5

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)){ } } September 3, 2020, 10:47pm 6

Put an int in front of redbutton

September 3, 2020, 11:38pm 7

See the following FAQ:

  • How to use button to start program
  • How to use button to start/stop the loop
September 4, 2020, 5:06am 8

My example

digitalRead(pinNumber);

Your code

while(digitalRead(redbutton == HIGH)){

spot the difference

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

Where is redbuttoninput declared ?

Topic Replies Views Activity
While loop not working Programming 7 113 March 13, 2025
While loop not working Programming 16 2044 November 20, 2022
Basics Programming 9 893 May 5, 2021
My code repeat only 1 time if I don't leave the button pressed. How I can fix it? Programming 6 149 October 22, 2024
Digital input with a pushbutton General Electronics 23 492 November 28, 2024
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino Pause Loop Until Button Pressed