# Help with breaking an infinite loop

**URL:** <https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088>\
**Category:** General Guidance\
**Created:** [June 4, 2026, 9:18am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088 "2026-06-04T09:18:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![atmnsm678413](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/atmnsm678413/32/1273736_2.png) [@atmnsm678413](https://forum.arduino.cc/u/atmnsm678413)\
**Post date:** [June 4, 2026, 9:18am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/1 "2026-06-04T09:18:13Z")

</div>

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

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

```

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

```arduino
 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

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [June 4, 2026, 9:36am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/2 "2026-06-04T09:36:11Z")

</div>

Please show the entire code you are trying to debug.

---

<div class="post-metadata">

**Author:** ![atmnsm678413](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/atmnsm678413/32/1273736_2.png) [@atmnsm678413](https://forum.arduino.cc/u/atmnsm678413)\
**Post date:** [June 4, 2026, 9:41am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/3 "2026-06-04T09:41:07Z")

</div>

```arduino
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;
    }
  
  }

 
}

```

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [June 4, 2026, 10:17am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/4 "2026-06-04T10:17:50Z")

</div>

_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?

---

<div class="post-metadata">

**Author:** ![atmnsm678413](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/atmnsm678413/32/1273736_2.png) [@atmnsm678413](https://forum.arduino.cc/u/atmnsm678413)\
**Post date:** [June 4, 2026, 10:25am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/5 "2026-06-04T10:25:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikedb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikedb/32/1098613_2.png) [@mikedb](https://forum.arduino.cc/u/mikedb)\
**Post date:** [June 4, 2026, 10:38am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/6 "2026-06-04T10:38:24Z")

</div>

> [@atmnsm678413](#):
>
> ```arduino
> while (digitalRead(button) == HIGH){ //while loop that stops everything until user presses the button to start
>   
>     
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 4, 2026, 10:40am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/7 "2026-06-04T10:40:09Z")

</div>

@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.

```arduino
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;
  }
  */
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [June 4, 2026, 10:40am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/8 "2026-06-04T10:40:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [June 4, 2026, 10:42am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/9 "2026-06-04T10:42:56Z")

</div>

> [@atmnsm678413](#):
>
> should I also incorporate inside of those while loops the if statement with the break?

Try it and see what happens.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 4, 2026, 11:13am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/10 "2026-06-04T11:13:09Z")

</div>

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.

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

```

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

```arduino
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);

```

---

<div class="post-metadata">

**Author:** ![mikedb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikedb/32/1098613_2.png) [@mikedb](https://forum.arduino.cc/u/mikedb)\
**Post date:** [June 4, 2026, 11:23am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/11 "2026-06-04T11:23:41Z")

</div>

> [@GolamMostafa](#):
>
> `If`

???

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 4, 2026, 11:37am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/12 "2026-06-04T11:37:51Z")

</div>

Thanks for pointing the typo.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 4, 2026, 8:46pm UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/13 "2026-06-04T20:46:23Z")

</div>

```arduino
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
}

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 5, 2026, 4:20am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/14 "2026-06-05T04:20:12Z")

</div>

> [@xfpd](#):
>
> `while (digitalRead(buttonpin)) {}; // wait for button press`

What is the purpose of ; in the above?

There are --

`do{}while();`

and

`while(){}`

and not

`while(){};`

> [@xfpd](#):
>
> ` while (1); // stay here forever`

Why not `while(true);?`

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [June 5, 2026, 4:24am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/15 "2026-06-05T04:24:07Z")

</div>

> [@GolamMostafa](#):
>
> > [@xfpd](#):
> >
> > `while (digitalRead(buttonpin)) {}; // wait for button press`
> 
> What is the purpose of ; in the above?

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

Now I watch as your post changes...

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/6/f/26ff8e735d04ac58fedd56b0dabf376f6e738863.png)

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [June 5, 2026, 4:34am UTC](https://forum.arduino.cc/t/help-with-breaking-an-infinite-loop/1447088/16 "2026-06-05T04:34:10Z")

</div>

> [@xfpd](#):
>
> > [@GolamMostafa](#):
> >
> > > [@xfpd](#):
> > >
> > > `while (digitalRead(buttonpin)) {}; // wait for button press`
> > 
> > What is the purpose of ; in the above?
> 
> To make little boys and non-sequitur bears ask questions.

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