Author Topic: How to wait for user to press a button Arduino  (Read 3466 times)

0 Members and 1 Guest are viewing this topic.

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
How to wait for user to press a button Arduino
« on: October 10, 2016, 02:05:36 am »
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 flags
   button = ReadButtons();
   //blank the demo text line if a new button is pressed or released, ready for a new label to be written
   if( 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");
   }
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: How to wait for user to press a button Arduino
« Reply #1 on: October 10, 2016, 03:02:22 am »
Please take the time to format your code in a readable way, so we don't have to spend time parsing it.

Use the
Code: [Select]
# 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? :)
« Last Edit: October 10, 2016, 03:05:38 am by alexanderbrevig »
 
The following users thanked this post: bar420

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Need help ending while loop in order for it to move on
« Reply #2 on: October 10, 2016, 05:45:57 am »
    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 can move 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 button
 while ( button == BUTTON_SELECT){}
while(button == 0){}
//program below
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;

  }
  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 )
  {
 

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #3 on: October 10, 2016, 05:48:41 am »
Sorry for any typos or confusion i'm very tired.
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3642
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #4 on: October 10, 2016, 06:07:35 am »
for (; test;) {x} and while(test) {x} do exactly the same thing. It's simpler to pick a style and stick with it instead of being inconsistent.
The reason your for loop never exits is that the code within it never changes button. 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.
 
The following users thanked this post: bar420

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
When Pressing buttons within the While Loop nothing happens
« Reply #5 on: October 10, 2016, 06:41:11 am »
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 button


Code: [Select]
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");
   [b] while (button == BUTTON_SELECT){} //I replaced : while (digitalRead( BUTTON_ADC_PIN ) == 0) {}[/b]
    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:
      {

      }
  }




 
« Last Edit: October 10, 2016, 07:00:32 am by bar420 »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: When Pressing buttons within the While Loop nothing happens
« Reply #6 on: October 10, 2016, 07:11:33 am »
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 button


Code: [Select]
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;     <================================= what is this supposed to do?  button is a variable, not a function

  }
  if (button == BUTTON_SELECT)
  {
    lcd.setCursor(0, 0);
    lcd.print("Press up");
    while (button == BUTTON_SELECT){}    <=================== if button == BUTTON_SELECT this loop will never exit and button isn't getting updated in the loop
    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:
      {

      }
  }


A couple of embedded comments...
« Last Edit: October 10, 2016, 07:14:44 am by rstofer »
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3642
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #7 on: October 10, 2016, 07:25:38 am »
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 declared volatile.
 

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #8 on: October 10, 2016, 07:47:29 am »
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 button


Code: [Select]
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;     <================================= what is this supposed to do?  button is a variable, not a function

  }
  if (button == BUTTON_SELECT)
  {
    lcd.setCursor(0, 0);
    lcd.print("Press up");
    while (button == BUTTON_SELECT){}    <=================== if button == BUTTON_SELECT this loop will never exit and button isn't getting updated in the loop
    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:
      {

      }
  }


A couple of embedded comments...
What should I do so that button is updated after I press a button while in the loop?
« Last Edit: October 10, 2016, 07:53:10 am by bar420 »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: How to wait for user to press a button Arduino
« Reply #9 on: October 10, 2016, 07:51:13 am »
Yea I get that i'm asking how I would be able to press a button while in the loop so it skips to next part. How do I change the variable when its in the loop then?

Code: [Select]
button = ReadButtons();
 
The following users thanked this post: bar420

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #10 on: October 10, 2016, 07:55:05 am »
where should I put that?
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: How to wait for user to press a button Arduino
« Reply #11 on: October 10, 2016, 07:57:10 am »
where should I put that?

Inside the while-loop.
 
The following users thanked this post: bar420

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #12 on: October 10, 2016, 08:00:42 am »
I just tried it but now its just skipping the while loop.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: How to wait for user to press a button Arduino
« Reply #13 on: October 10, 2016, 08:04:22 am »
I just tried it but now its just skipping the while loop.

You probably want to check that the while-loop condition is actually what you want to achieve.
 
The following users thanked this post: bar420

Offline bar420Topic starter

  • Newbie
  • Posts: 8
  • Country: us
Re: How to wait for user to press a button Arduino
« Reply #14 on: October 10, 2016, 08:07:22 am »
where should I put that?

Inside the while-loop.

Never mind I just got it thanks your a lifesaver.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf