How To Use Button To Start/stop The Loop | Arduino FAQs
Maybe your like
- TUTORIALS
- HARDWARE & TOOLS
- REFERENCES
- FAQs
- ABOUT US
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.
AnswerLet's see and compare the code WITHOUT and WITH the start/stop button.
Arduino Code WITHOUT the Start/Stop Button.
void setup() { /******************* * your setup code *******************/ } void loop() { /****************** * your loop code ******************/ }Arduino Code WITH the Start/Stop Button.
#include <ezButton.h> #define LOOP_STATE_STOPPED 0 #define LOOP_STATE_STARTED 1 ezButton button(7); // create ezButton object that attach to pin 7; int loopState = LOOP_STATE_STOPPED; void setup() { /******************* * your setup code *******************/ button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if (button.isPressed()) { if (loopState == LOOP_STATE_STOPPED) loopState = LOOP_STATE_STARTED; else // if(loopState == LOOP_STATE_STARTED) loopState = LOOP_STATE_STOPPED; } if (loopState == LOOP_STATE_STARTED) { /****************** * your loop code ******************/ } }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 |
The Best Arduino Starter Kit
- See the best Arduino kit for beginner
See Also
- How to know value of resistor
- fatal error: library.h: No such file or directory
- How to know I2C address of sensor/device
- DS3231 vs DS1307
- Arduino - How to detect button press event
※ OUR MESSAGES
- We are AVAILABLE for HIRE. See how to hire us to build your project
- If this tutorial is useful for you, please give us motivation to make more tutorials.
Tag » Arduino Pause Loop Until Button Pressed
-
Pause Code Untill A Button Is Pressed - Arduino Stack Exchange
-
How To Pause And Continue A Loop With A Button Press?
-
Pause Code Until Button Is Pressed. - Arduino Forum
-
Pause Program Mid Loop Until Switch Is Pressed? - Arduino Forum
-
How To Pause Code Until A Button Is Pressed (Arduino Uno ... - Quora
-
Is There A Way To Pause And Resume Void Loop In Arduino Using Two ...
-
[Arduino] ButtonWait - Wait For Button Press Before Further Execution.
-
Wait For A Button Press, Then Continue : R/arduino - Reddit
-
Arduino Continue With For-loop Only When Button Is Pressed
-
Trying To Pause The Code Until My Button Is Pressed And Will Continue ...
-
The Basics Of Arduino Programming: Loops, Conditions, Objects ...
-
Pausing A Arduino Shetch, And Wait For A Button Press - EEVblog
-
Tutorial_pushbutton_debounce_i...
-
Detecting Other Conditions - Code: Robotics