Can't break a while loop in Arduino

Thread Starter

M3D0

Joined Oct 8, 2020
50
Hello Everyone! For the last two days, I've been trying to write a code that keeps doing/activating a LED till a button is pushed to get out of the while loop and continue the rest of the code. I have tried the following approaches to solve this problem and didn't work:
1)creating another void function that is being called in while loop
2)putting an if condition inside the while loop to check again if the button is pressed or not to execute [break; ]
3) using "do ...while" loop

(N.B: I did simplify the theory of operation as the button in the original code will be a sensor that has a digital output, I just want to focus on how do I get out of the loop and continue executing the rest of the code)
Thanks in advance!

simple Arduino while loop code:
const int reqg = 3; //button pin

int BUTTONstateg = 0; //button state


const int LED1 = 7; // led1 pin
const int LED2 =8 ; // led2 pin



void setup() {

  pinMode(reqg, INPUT); //setting the buuton pin as an input

  pinMode(LED1, OUTPUT); //setting the buuton pin as an input
  pinMode(LED2, OUTPUT); //setting the buuton pin as an input

  Serial.begin(9600);// setup Serial Monitor to display information


}

void loop (){
  BUTTONstateg = digitalRead(reqg); //getting reading from the button

   
  while (BUTTONstateg == LOW ){
    delay(1000);
    digitalWrite(LED1, HIGH); //LED1 is on till the button is pressed
    Serial.println("in while loop");
   
  }
  digitalWrite(LED2, HIGH);//LED2 supposed to be on when the button is pressed so the while loop is no longer being executed
  Serial.println("out from while loop (which I can't reach it)");




}
arduinoschem.PNG
 

djsfantasi

Joined Apr 11, 2010
7,187
Hello Everyone! For the last two days, I've been trying to write a code that keeps doing/activating a LED till a button is pushed to get out of the while loop and continue the rest of the code. I have tried the following approaches to solve this problem and didn't work:
1)creating another void function that is being called in while loop
2)putting an if condition inside the while loop to check again if the button is pressed or not to execute [break; ]
3) using "do ...while" loop

(N.B: I did simplify the theory of operation as the button in the original code will be a sensor that has a digital output, I just want to focus on how do I get out of the loop and continue executing the rest of the code)
Thanks in advance!

simple Arduino while loop code:
const int reqg = 3; //button pin

int BUTTONstateg = 0; //button state


const int LED1 = 7; // led1 pin
const int LED2 =8 ; // led2 pin



void setup() {

  pinMode(reqg, INPUT); //setting the buuton pin as an input

  pinMode(LED1, OUTPUT); //setting the buuton pin as an input
  pinMode(LED2, OUTPUT); //setting the buuton pin as an input

  Serial.begin(9600);// setup Serial Monitor to display information


}

void loop (){
  BUTTONstateg = digitalRead(reqg); //getting reading from the button

  
  while (BUTTONstateg == LOW ){
    delay(1000);
    digitalWrite(LED1, HIGH); //LED1 is on till the button is pressed
    Serial.println("in while loop");
  
  }
  digitalWrite(LED2, HIGH);//LED2 supposed to be on when the button is pressed so the while loop is no longer being executed
  Serial.println("out from while loop (which I can't reach it)");




}
View attachment 227632
First, there is a command to force an exit from loops. Look at the break command.

BUT, that’s not your problem. Once you enter the whole loop, you never check the button state again! So, of course you’ll never exit.

Insert a digital read of the button inside the while loop. You’ll need a second read to check the button state. You’ll still need the first read to get the initial button state.
 

ErnieM

Joined Apr 24, 2011
8,076
The main void loop(void) is already an endless loop so there is no need to create another.

I would write the loop like so:

C:
void loop (){
  if (digitalRead(reqg) == LOW)    // Check the button
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    Serial.println("Button LOW");
  }
  else
  {
    digitalWrite(LED1, LOW)
    digitalWrite(LED2, HIGH)
    Serial.println("Button HIGH");
  }
  delay(1000);
}
 
Hello Everyone! For the last two days, I've been trying to write a code that keeps doing/activating a LED till a button is pushed to get out of the while loop and continue the rest of the code. I have tried the following approaches to solve this problem and didn't work:
1)creating another void function that is being called in while loop
2)putting an if condition inside the while loop to check again if the button is pressed or not to execute [break; ]
3) using "do ...while" loop

(N.B: I did simplify the theory of operation as the button in the original code will be a sensor that has a digital output, I just want to focus on how do I get out of the loop and continue executing the rest of the code)
Thanks in advance!

simple Arduino while loop code:
const int reqg = 3; //button pin

int BUTTONstateg = 0; //button state


const int LED1 = 7; // led1 pin
const int LED2 =8 ; // led2 pin



void setup() {

  pinMode(reqg, INPUT); //setting the buuton pin as an input

  pinMode(LED1, OUTPUT); //setting the buuton pin as an input
  pinMode(LED2, OUTPUT); //setting the buuton pin as an input

  Serial.begin(9600);// setup Serial Monitor to display information


}

void loop (){
  BUTTONstateg = digitalRead(reqg); //getting reading from the button

  
  while (BUTTONstateg == LOW ){
    delay(1000);
    digitalWrite(LED1, HIGH); //LED1 is on till the button is pressed
    Serial.println("in while loop");
  
  }
  digitalWrite(LED2, HIGH);//LED2 supposed to be on when the button is pressed so the while loop is no longer being executed
  Serial.println("out from while loop (which I can't reach it)");




}
View attachment 227632
Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6
 
Top