# Delay() in main loop preventing ISR button detection

**URL:** <https://forum.arduino.cc/t/delay-in-main-loop-preventing-isr-button-detection/1148422>\
**Category:** Programming\
**Created:** [July 15, 2023, 6:23pm UTC](https://forum.arduino.cc/t/delay-in-main-loop-preventing-isr-button-detection/1148422 "2023-07-15T18:23:00Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 15, 2023, 6:50pm UTC](https://forum.arduino.cc/t/delay-in-main-loop-preventing-isr-button-detection/1148422/4 "2023-07-15T18:50:59Z")

</div>

So the wanted functionality is you have a **constant** sequence of steps switching some IO-pins HIGH/LOW after different periods of time.

and whenever you press the button stop the sequence

This is best coded using lines of code that build a so called state-machine  
state 1: wait 5000 milliseconds have passed by  
state 2: switch motorbrake HIGH  
state 3: wait 4000 milliseconds have passed by  
state 4: switch switch motor-brake LOW  
state 5: wait until 3000 milliseconds have passed by  
state 6: switch e14 HIGH  
state 7: wait 2000 milliseconds have passed by  
state 8: switch e14 LOW  
state 9: wait 4000 milliseconds have passed by  
state 10: switch motorbrake HIGH  
state 11: wait 4000 milliseconds have passed by  
state 12: switch switch motor-brake LOW  
state 13: wait 2000 milliseconds have passed by  
state 14: switch e12 HIGH  
state 15: wait 13000 milliseconds have passed by  
state 16: switch e12 LOW and increase variable counter by one

writing this code uses the switch-case- **break;** statement  
the **break;** is very important to make it work as intended

In your case The conditions to proceed to the next state is

- either immitiately change to next state  
or
- change to the next state after a number of milliseconds have passed by

I have written a tutorial about state-machines

> [@A Demo-Code explaining the switch-case state-machine and how to do things (almost) in parallel](https://forum.arduino.cc/t/a-demo-code-explaining-the-switch-case-state-machine-and-how-to-do-things-almost-in-parallel/888172/1):
>
> Hi everybody, if you look at the end of this first posting you see a list of links that show where this tutorial was used as a link. Go through this list to find additional information on how to apply this code This democode requires only the pure microcontroller without any hardware. You just open the serial monitor to watch the behaviour of the code. From time to time I enjoy writing demo-codes that want to explain programming-functionalities. There are a lot of different approachs to expl…

best regards Stefan

---

_[View the full topic](https://forum.arduino.cc/t/delay-in-main-loop-preventing-isr-button-detection/1148422)._
