How to wait for user to press a button Arduino - Page 1
Forum section: electronics / general

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 02:05:36 am
content: How to wait for a user to press the button on the arduino it gets to the while loop and then constantly loops not allowing me to press a button. The "good job" print is to check where the program is getting stuck.void loop(){byte questions;byte button;byte timestamp;static int count = 0;static int mode = 0;static bool bRefresh = true;static int score=0;static int options=5;int answer=1;;//get the latest button pressed, also the buttonJustPressed, buttonJustReleased flagsbutton = ReadButtons();//blank the demo text line if a new button is pressed or released, ready for a new label to be writtenif( buttonJustPressed || buttonJustReleased ){button;}for(;button == BUTTON_SELECT;){lcd.setCursor(0,0);lcd.print("Press up");while(digitalRead( BUTTON_ADC_PIN ) == HIGH) {}if(button == BUTTON_UP){}lcd.print("good job");}

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: October 10, 2016, 03:02:22 am
content: Please take the time to format your code in a readable way, so we don't have to spend time parsing it.Use theCode:#icon in the toolbar when you edit your post.EDIT: A quick scan reveals that your loop is not complete. This line:while(digitalRead( BUTTON_ADC_PIN ) == HIGH) {}would halt until it reads LOW. So I guess you've kind of answered your own question?

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 05:45:57 am
content: I got it to halt but I don't know what I need it to do in order to change it to low to make it so I canmove on to the next if statement.It keeps halting and repeating the infinite while loop when I need it to be able to move on to the next part of the program when a press another button.The arduino is not checking for other buttons being pressed after the initial "Button Select" pressed in the beginning. The up button or any other button is not registering with the arduino during the while statement instead repeating forever.//Other while statements I have used, I have tried countless things and I just really need someone to clear up why nothing happens after pressing the initial buttonwhile ( button == BUTTON_SELECT){}while(button == 0){}//program belowvoid loop(){byte questions;byte button;byte timestamp;static int count = 0;static int mode = 0;static bool bRefresh = true;static int score = 0;static int options = 5;int answer = 1;button = ReadButtons();if ( buttonJustPressed || buttonJustReleased ){button;}for (; button == BUTTON_SELECT;){lcd.setCursor(0, 0);lcd.print("Press up");while (digitalRead( BUTTON_ADC_PIN ) == 0) {}if (button == BUTTON_UP){}lcd.print("good job");}switch ( button ){

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 05:48:41 am
content: Sorry for any typos or confusion i'm very tired.

author: helius
author_rank: Super Contributor
timestamp: October 10, 2016, 06:07:35 am
content: for (; test;) {x}andwhile(test) {x}do exactly the same thing. It's simpler to pick a style and stick with it instead of being inconsistent.The reason yourforloop never exits is that the code within it never changesbutton. And that variable won't change by itself: it doesn't know anything about the current state of the pin inputs, it's just a variable.

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 06:41:11 am
content: no the while loop never exits. I don't want the for loop to exit I want it to repeat until the statement is false. Also I changed it to a for loop because I was just trying a lot of things because I was stuck. Also how do I change the variable?I'm pressing the buttons on the LCD but they are not registering other than the original "BUTTON_SELECT"while (digitalRead( BUTTON_ADC_PIN ) == 0) {} //This loop never exits using this line of code to halt the program until I press another buttonCode:void loop(){byte questions;byte button;byte timestamp;static int count = 0;static int mode = 0;static bool bRefresh = true;static int score = 0;static int options = 5;int answer = 1;button = ReadButtons();if ( buttonJustPressed || buttonJustReleased ){button;}if (button == BUTTON_SELECT){lcd.setCursor(0, 0);lcd.print("Press up"); while (button == BUTTON_SELECT){} //I replaced : while (digitalRead( BUTTON_ADC_PIN ) == 0) {}if (button == BUTTON_UP){}lcd.print("good job");}switch ( button ){case BUTTON_SELECT:{answer = 1;break;}case BUTTON_DOWN:{break;}case BUTTON_UP:{break;}case BUTTON_NONE:{}}

author: rstofer
author_rank: Super Contributor
timestamp: October 10, 2016, 07:11:33 am
content: Quote from: bar420 on October 10, 2016, 06:41:11 amA couple of embedded comments...

author: helius
author_rank: Super Contributor
timestamp: October 10, 2016, 07:25:38 am
content: while (button == BUTTON_SELECT){}checks if the test is true, then if it's true, executes the body (which is empty), then checks if the test is true, then if it's true, executes the body.... et cetera. When the test is just a variable equal to something, it always stays the same*, so this is an infinite loop.* unless the variable is changed by another thread or a signal handler or an ISR and is declaredvolatile.

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 07:47:29 am
content: Quote from: rstofer on October 10, 2016, 07:11:33 amWhat should I do so that button is updated after I press a button while in the loop?

author: Kalvin
author_rank: Super Contributor
timestamp: October 10, 2016, 07:51:13 am
content: Quote from: bar420 on October 10, 2016, 07:47:29 amCode:button = ReadButtons();

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 07:55:05 am
content: where should I put that?

author: Kalvin
author_rank: Super Contributor
timestamp: October 10, 2016, 07:57:10 am
content: Quote from: bar420 on October 10, 2016, 07:55:05 amInside the while-loop.

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 08:00:42 am
content: I just tried it but now its just skipping the while loop.

author: Kalvin
author_rank: Super Contributor
timestamp: October 10, 2016, 08:04:22 am
content: Quote from: bar420 on October 10, 2016, 08:00:42 amYou probably want to check that the while-loop condition is actually what you want to achieve.

author: bar420
author_rank: Newbie
timestamp: October 10, 2016, 08:07:22 am
content: Quote from: Kalvin on October 10, 2016, 07:57:10 amNever mind I just got it thanks your a lifesaver.