There are many ways you can stop the Arduino program where necessary and I am sure you must be aware with the famous resetting button way to stop the program, but have you ever thought that how many ways you can stop the Arduino program without interrupting your hardware? Then, bear with me.
Yes, there are many ways you can stop the program such as use the exit function, reset button or tap it in a loop.
I have compiled a list of the ways to stop the program and they are all based on my personal experience and have worked on my projects every time. I have never felt difficulty doing that.
If you are not aware with the software what it is called that, is Arduino IDE (Integrated Development Environment). It is open-source software where you can write programs on your own and control components. This software offers a live debugger, code navigation, and auto-completion, so users can easily write code and upload everything to the circuit board for interaction. However, if you have written the code but don’t know how to stop the program, we are sharing a few options with you!
Table of Contents
1. Use The Exit Function
One of the easiest ways is to use the exit function. For this purpose, you only have to write “exit(0);” in the coding area, and the program will end. Many people use it to end the loop, but it doesn’t work on loops. On the other hand, if you have to exit a void function, it’s recommended that you use “return;.”
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
// Add a condition to check if the program should stop
if (some_condition) {
exit(0);
}
// more code here
}
void someFunction() {
// put your code here
// Add a condition to check if the function should stop
if (some_condition) {
return;
}
// more code here
}
The exit function is used to stop the loop function and the return statement is used to stop the someFunction function. The argument 0 passed to the exit function indicates that the program stops with no errors. If you want to indicate that the program is stopped due to an error, you can pass a non-zero value as the argument to the exit function.
Note that the exit function is not part of the Arduino libraries and needs to be imported from the standard cstdlib library. To use the exit function, you need to add the following line at the top of your sketch:
#include <cstdlib>
Irrespective of which function you use, you must add it at the end of the code because if you write anything after it, it won’t work properly.
2. Use The Void Function
Void is another function that is very helpful, especially if you want to stop the running program rather than disconnect power from the circuit board. In addition, it uses to stop the never-ending loop. For this purpose, you can use the “void()” function.
It’s a great option for users who need automation because it automatically leaves the function when the code is complete.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
// Add a condition to check if the loop should stop
if (some_condition) {
stopLoop();
}
// more code here
}
void stopLoop() {
// put any cleanup code here
// stop the loop
return;
}
The loop function is the main loop of the Arduino program. The stopLoop function is used to stop the loop. When the some_condition is met, the stopLoop function is called, which stops the loop.
The stopLoop function can contain any code that needs to be executed before stopping the loop, such as cleaning up memory, closing files, etc. Once the code in the stopLoop function is complete, the return statement is used to stop the function and return control to the caller (the loop function in this case).
Note that the void function is a built-in type in C++ and does not require any special libraries or imports to use.
3. Sleep Mode
The third option is to implement sleep mode. It allows the user to turn off or stop the modules in a microcontroller that aren’t currently in use. It’s great for minimizing power consumption. To use this, you’ve got to turn on the sleep bit in the SMCR.SE, which is the sleep mode control register.
With this mode, the CPU will stop extracting the instructions, and the peripherals will also stop working, including the ADCs and timers. Also, when the sleep mode is disabled with an interruption, the program is continued according to the previous instructions. To use this option, just add “sleep()” at the end of the code – it will instruct Arduino to quit the operations unless you have timer-set tasks.
#include <avr/sleep.h>
#include <avr/power.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
// Add a condition to check if the sleep mode should be enabled
if (some_condition) {
enterSleepMode();
}
// more code here
}
void enterSleepMode() {
// turn off ADC and timers
ADCSRA &= ~(1 << ADEN);
power_all_disable();
// set sleep mode to "power-down"
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// sleep until an interrupt occurs
sleep_cpu();
// disable sleep
sleep_disable();
// turn ADC and timers back on
power_all_enable();
ADCSRA |= (1 << ADEN);
}
The loop function is the main loop of the Arduino program. The enterSleepMode function is used to enable sleep mode. When the some_condition is met, the enterSleepMode function is called, which enables sleep mode.
The enterSleepMode function disables the ADC and timers, sets the sleep mode to “power-down” using the set_sleep_mode function, and enables sleep using the sleep_enable function. Then, the sleep_cpu function is called to put the CPU to sleep.
Note that the sleep function mentioned in the paragraph is not a built-in function in the Arduino language, but the sleep_cpu function from the avr/sleep.h library is used instead.
4. Use The Setup Paragraph
Many people complain about running never-ending loops after stopping the programs. This is why I recommend putting the program in the setup program, and the loop paragraph should be left empty. As a result, the program will run once and then stop.
Here’s an example to illustrate this:
void setup() {
// Initialize things here
}
void loop() {
// Leave the loop empty
}
To stop the program after it has run once, you need to add a return statement at the end of the setup function:
void setup() {
// Initialize things here
return;
}
void loop() {
// Leave the loop empty
}
With this code, the program will run the setup function once, then it will stop and wait for a reset.
5. Input Pin
An input pin is basically an interface between the second circuit and a microcontroller. The users can configure these pins with the help of the pinMode() function. This pin can be used to stop the program – we recommend checking the pin’s level by polling the loop code or interrupting the code. In addition, when the input pin detects a change in the voltage, the code will detect the change, and the program will be stopped.
const int stopButton = 2; void setup() { pinMode(stopButton, INPUT); } void loop() { if (digitalRead(stopButton) == LOW) { return; } // Your program code here }
In this example, the stopButton constant is used to define the pin number that is connected to the input button. In the setup function, the pin is set as an input using pinMode (stopButton, INPUT). In the loop function, the voltage level of the pin is checked using digitalRead (stopButton). If the voltage level is LOW, then the program stops by executing the return statement.
Note: You can also use an interrupt to detect a change in the input pin voltage level, but that requires a little more complex setup, and it is beyond the scope of this answer.
6. Serial Communication
Serial communication is a widely used method that’s used by the board to communicate with other devices, such as a peripheral. Luckily, serial communication can be used to control the programs, including stopping the programs. All you’ve to do is leave the serial communication tab blank to make sure the circuit board doesn’t get any instructions, resulting in the program halting.
void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { int inByte = Serial.read(); if (inByte == 's') { return; } } // Your program code here }
In this example, the setup function starts the serial communication at a baud rate of 9600 using Serial.begin(9600). In the loop function, the Serial.available() function is used to check if there is any incoming serial data. If there is, the code reads the first byte of the data using Serial.read() and checks if it’s equal to the character ‘s’. If it is, the program stops by executing the return statement.
Note: The code assumes that the data being sent over serial is a single character, and that the character ‘s’ is used to stop the program. You can change this to suit your needs.
7. Turn Off The Power
One of the most convenient methods is to turn off power in a way that allows safe disconnection of the software. It’s important to disconnect the software carefully from the power source. When the circuit board is disconnected, it will stop operating the programs as well as their memories. In addition, the data stored in the program memory and EEPROM will be reset.
When the power connection is restored, the program will be resumed automatically according to the recent command or sketch. The good thing is that program variables will be kept in the RAM.
8. Reset Button
The circuit board/hardware has a reset button on the side, which can be used to turn off the program and bring the circuit board back to the factory default settings. When the reset button is pressed for ten seconds, the microcontroller will turn off, and when you release it, the microcontroller will resume executing the leftover command or sketch from the setup paragraph.
The reset button will halt the current program from running, and the data stored in RAM will be deleted. However, the data stored in the EEPROM will be saved.
9. Tap It In A Loop
If you want to stop the program from executing a code, you can tap it in a loop. For this purpose, you can enter conditional statements, use triggering interrupts, or set up timers. For instance, if you want to customize when the code is executed according to the incoming data and time, you have to add conditional checks and incorporate them into the code.
bool stopProgram = false;
void setup() {
// Initialize things
In this example, the stopProgram variable is used to control whether the program should stop or not. In the loop function, the code checks if stopProgram is true. If it is, the program stops by executing the return statement. You can change the value of stopProgram anywhere in the code to stop or start the program.
unsigned long stopTime = 0;
void setup() { // Initialize things here } void loop() { if (millis() >= stopTime) { return; } // Your program code here }
In this example, the stopTime variable is used to store the time at which the program should stop. In the setup function, you would set stopTime to the desired stop time by adding the desired number of milliseconds to the value returned by millis(). In the loop function, the code checks if the current time, as given by millis(), is greater than or equal to stopTime. If it is, the program stops by executing the return statement.
Note: The millis() function returns the number of milliseconds since the program started, so it’s a good way to keep track of time in an Arduino program.
Conclusion
Stopping the programs in Arduino is pretty convenient, and using the exit and void commands are the quickest options. However, if nothing works for you, you can simply load a new program, and the previous one will be flushed out. The only downside of this method is that you cannot resume the previous program or access it. On the other hand, you can also assign a kill switch in the code!