How to use button to start/stop the loop
How can I start the loop if a button is pressed, stop the loop if the button is pressed again in Arduino? This process is repeated forever.
Let's see and compare the code WITHOUT and WITH the start/stop button.
Arduino Code WITHOUT the Start/Stop Button.
Arduino Code WITH the Start/Stop Button.
The wiring diagram for above code:
This image is created using Fritzing. Click to enlarge image
If you want to use a button to start the program only when the button is pressed the first time, see Arduino - using a button to start the program
※ NOTE THAT:
- In this case, we SHOULD debounce the button. If not, the code may not work as expected.
- The above code uses the ezButton library, you can see how to install the library
Hardware for above code
|1 × Arduino UNO Buy on Amazon
|1 × USB 2.0 cable type A/B Buy on Amazon
|1 × Button Buy on Amazon
|1 × Breadboard Buy on Amazon
|1 × Jumper Wires Buy on Amazon