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!

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