Help with breaking an infinite loop

Hey guys, I am trying to make a circuit that loops infinitely once a button is pressed but if the button is pressed again, I would like it to stop. For the infinite loop, I am using a while loop, I tried initially doing the same method as seen at the start of

while (digitalRead(button) == HIGH){  
    
    }

This works to start the loop but the loop itself it doesn't stop it, then I tried the following:

 while (1==1){ //while loop that stops everything until user presses the button to start
  
    
    
  
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <=20000) // do this loop for up to 1000mS = 1S, 20x60x1000=1200
  {
    digitalWrite(redLED, LOW);
    delay(500);
    digitalWrite(redLED, HIGH);
    delay(500);
    endtime = millis();
  }

  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);

 starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <=5000) // do this loop for up to 1000mS = 1S
  {
    digitalWrite(redLED, HIGH);
    endtime = millis();
  }
 
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  digitalWrite(redLED, LOW);
  delay(50);

  if (digitalRead(button)==LOW){
    break;
    }
}

but this doesn't work either. I believe it could be because the circuit is not actively listening to the button, it is going through the circuit so please if anyone could suggest a solution to have like the circuit also constantly listening at the signals sent by the button.

Thanks in advance to all contributions

Please show the entire code you are trying to debug.

int redLED = 3;
int blueLED = 4;
int button = 6;
int buzzer = 5;

long starttime = 0;
long endtime = 0;



void setup() {
 Serial.begin(9600); //remove when end
 pinMode(A0, INPUT); //potentiometer
 pinMode(buzzer, OUTPUT);
 pinMode(redLED, OUTPUT);
 pinMode(blueLED, OUTPUT);
 pinMode(button, INPUT_PULLUP);
 
 digitalWrite(redLED, LOW); //initialize all these as off, LEDs as well as we need to see in the void loop which one will be turne on
 digitalWrite(blueLED, LOW);
 digitalWrite(buzzer, LOW);

}

void loop() {

  //depending on if manual or auto, have either one or the other LED on
  
  while (digitalRead(button) == HIGH){ //while loop that stops everything until user presses the button to start
  
    
    }
 digitalWrite(buzzer, HIGH);
  delay(50); //delay required between while loops or the LOW state that breaks each loop will affect both and will not start the light flashing loop
  digitalWrite(buzzer, LOW);

  
  while (1==1){ //while loop that stops everything until user presses the button to start
  
    
    
  
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <=20000) // do this loop for up to 1000mS = 1S, 20x60x1000=1200
  {
    digitalWrite(redLED, LOW);
    delay(500);
    digitalWrite(redLED, HIGH);
    delay(500);
    endtime = millis();
  }

  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);

 starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <=5000) // do this loop for up to 1000mS = 1S
  {
    digitalWrite(redLED, HIGH);
    endtime = millis();
  }
 
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  digitalWrite(redLED, LOW);
  delay(50);

  if (digitalRead(button)==LOW){
    break;
    }
  
  }

 
}

while ((endtime - starttime) <= 20000)
Your program will stay in this while loop for 20 seconds so it does not know if the button was pushed

while ((endtime - starttime) <= 5000)
Your program will stay in this while loop for 5 seconds so it does not know if the button was pushed

So what do you think you should do?

should I also incorporate inside of those while loops the if statement with the break?

This line just keep on reading the button while the buttonState is HIGH and does nothing else.

@atmnsm678413

Your sketch is moderated as follows so that redLED blinks for 20 sec at 1 sec interval once you press the button. Now, read pos #4 @anon95813201 and add necessary code so that when the button is agian pressed within this 20 sec loop, the blinking will stop and the control will come out of while() loop.

int redLED = 3;
int blueLED = 4;
int button = 6;
int buzzer = 5;

//unsigned long startTime = 0;
//unsigned long endtTme = 0;
unsigned long pr1Millis = millis();
unsigned long pr2Millis = millis();
byte ledState = HIGH;

void setup() {
  Serial.begin(9600);  //remove when end
  pinMode(A0, INPUT);  //potentiometer
  pinMode(buzzer, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  digitalWrite(redLED, LOW);  //initialize all these as off, LEDs as well as we need to see in the void loop which one will be turne on
  digitalWrite(blueLED, LOW);
  digitalWrite(buzzer, LOW);
}

void loop() 
{

  //depending on if manual or auto, have either one or the other LED on

  while (digitalRead(button) == HIGH) 
  {  //while loop that stops everything until user presses the button to start

    ;  //wait
  }

  /* digitalWrite(buzzer, HIGH);
  delay(50);  //delay required between while loops or the LOW state that breaks each loop will affect both and will not start the light flashing loop
  digitalWrite(buzzer, LOW);


  while (1 == 1) {  //while loop that stops everything until user presses the button to start
    digitalWrite(buzzer, HIGH);
    delay(100);
    digitalWrite(buzzer, LOW);

    startTime = millis();
     endTime = starttime;
     while ((millis()) - starttime) <= 20000)  // 20 sec test time
     {
      */
  do  //blinking redLED for 20 sec at 1 sec interval
  {
    digitalWrite(redLED, ledState);
    if (millis() - pr1Millis >= 1000) 
    {
      ledState = !ledState;
      pr1Millis = millis();
    }
  } while (millis() - pr2Millis <= 20000);
  digitalWrite(redLED, LOW);
  /*
    digitalWrite(redLED, LOW);
    delay(500);
    digitalWrite(redLED, HIGH);
    delay(500);
    endtime = millis();
  
  }

  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);

  starttime = millis();
  endtime = starttime;
  while ((endtime - starttime) <= 5000)  // do this loop for up to 1000mS = 1S
  {
    digitalWrite(redLED, HIGH);
    endtime = millis();
  }

  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  digitalWrite(redLED, LOW);
  delay(50);

  if (digitalRead(button) == LOW) {
    break;
  }
  */
}

You should get rid of the delay() statements and replace with millis based timing loops, so that you can read the button input during those delays.

Are you wanting to exit the while() loop immediately after a button press, or do you want to complete the entire sequence and only exit at the end?

Try it and see what happens.

Try to sample the closing condition of button within the 20 sec loop of the sketch of post #7 (you have marked solution) for coming out of the while() loop. Try to put in the following code and post to show what you ave done.

if(digitalRead(button) == HIGH) // Edit: If ===> if 
{
    break;
}

The above should go in the while() loop of the following code.

do  //blinking redLED for 20 sec at 1 sec interval
  {
    digitalWrite(redLED, ledState);
    if (millis() - pr1Millis >= 1000) 
    {
      ledState = !ledState;
      pr1Millis = millis();
    }
  } while (millis() - pr2Millis <= 20000);
  digitalWrite(redLED, LOW);

???

Thanks for pointing the typo.

int buttonpin = 2; // pin >> button n.o. >> button n.o. >> gnd // PRESSED = LOW

void setup() {
  Serial.begin(115200);
  pinMode(buttonpin, INPUT_PULLUP);

  Serial.println("begin");
  while (digitalRead(buttonpin)) {};  // wait for button press
  Serial.println("start");
  delay(200);
}

void loop() {
  while (digitalRead(buttonpin)) { // wait for button press
    // stuff you want to do before "stop" button press.
  };
  Serial.println("stop");
  while (1); // stay here forever
}

What is the purpose of ; in the above?

There are --

do{}while();

and

while(){}

and not

while(){};

Why not while(true);?

To make little boys and non-sequitur bears ask questions.

Now I watch as your post changes...

If the ; is a C++ line termination, then say so!