Can't break a while loop in Arduino

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!

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)");




}

m3d0:

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");  
 }
}

How and when will BUTTONstateg become anything else than LOW ? There's nothing inside the loop that will ever change its value! The value of digitalRead(reqg) may change, but that is outside the loop and will not be detected. So... ? :wink:

PS.: Karma++ for a correct first post, ncluding a clear description of the problem and the use of code tags.

In general if you need to break out of a WHILE loop you should not be using WHILE. Instead use IF and allow loop() to do the repetition.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

1 Like

consider
... toggles LED1 on/off instead of just turning it off

int BUTTONstateg = 0; //button state

#if 1
const int reqg = 3; //button pin
const int LED1 = 7; // led1 pin
const int LED2 = 8 ; // led2 pin

#else   // my hardware
const int reqg = A1;
const int LED1 = 10;
const int LED2 = 11;
#endif

enum { Off = HIGH, On = LOW, };     // depends on hardware

void setup() {
    pinMode(reqg, INPUT_PULLUP); //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

    digitalWrite(LED1, On);
    digitalWrite(LED2, On);

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

void loop ()
{
    byte  but = digitalRead(reqg);

    if (BUTTONstateg != but)  {     // state change
        BUTTONstateg = but;
        if (On == but)              // button pressed
            digitalWrite(LED1, ! digitalRead (LED1));   // toggle LED
        delay (10);   // debounce
    }

    // no need to repeatedly turn on LED2
    // digitalWrite(LED2, HIGH);
}

One more very important thing you should consider:
When you press a button, it doesn't really behave the way you think it does. Instead of it just being HIGH and then LOW, something else happens:
The button has micro oscillations, and, in a few milliseconds, your signal will look something like this:
HIGH LOW HIGH LOW HIGH LOW etc. It just bounces on the conductor and makes your microcontroller think it's being pressed constantly.
So, what you need to do it: either add a delay, or, even better, use millis() to solve that problem, like gcjr said.

Thank you, everybody, the problem was that I was saving the reading of the button (HIGH) without updating it in the while loop, so the solution I put the condition in the while loop [ while (digitalRead(reqg) == LOW ) ]. Thanks again for your help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.