How to Stop Void Loop in Arduino?
Arduino sketches mainly consist of two parts one is setup and second one is loop. Setup functions only run once after the code is compiled while Loop function keeps on running until a new sketch or Arduino loses its power.
Multiple projects might demand void loop functions to run for a specific time or stop after performing certain instructions so now we will look at how we can stop Arduino Void Loop function using different programming techniques.
Does the loop Function Really Stop?
Arduino does not provide any way of stopping the void loop function. Besides Arduino there is no such thing to exist in microcontrollers in the first place. Technically all the techniques we used are just to halt the Arduino loop functionality. It does not stop completely, we either send void loop to an infinite condition using while or use an exit statement to halt its process. By doing this Arduino loop stuck inside an infinite loop without executing instructions, the only way to get it back is by resetting; through a button or by uploading a new sketch. So technically the Arduino void loop does not stop.
Methods to Stop void loop in Arduino
Arduino programming has two types of loops one which is by default in Arduino sketch which is void loop() while the second one is loop used during programming or writing a sketch. User created loops can easily stop using break statements.
Below mentioned are few steps to stop void loop in Arduino:
i: Using Infinite While Loop
To stop the void loop in Arduino, sketch an infinite loop can be used with the help of a while loop structure. This method will work on all Arduino boards, but this will not stop the Arduino functioning as Arduino continues to consume power. This method can easily be implemented in any code after the last line where your code is finished.
Let’s take an LED blink example to understand how the while loop is helpful in breaking the void loop function:
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
delay(1000); // delay of a second
digitalWrite(LED_BUILTIN, LOW); // turn off LED
delay(1000); // delay of a second
while(1){ // infinite loop
}
}
In the code above we have initialized the built in LED pin in void setup function after that in the void loop section an LED blinking program is written. Here the LED will start blinking in a pattern of one 1 second on and 1 second off. After one cycle is completed an infinite while loop is initiated at the end of the second delay; now the LED will remain turned off until the code is re-uploaded or Arduino is reset.
ii: Using the Sleep Library
In the above method Arduino void loop is stopped but Arduino will continue to take power. Next method which will permanently stop void loop and put Arduino in sleep mode is using Sleep_n0m1 library, using this Arduino CPU can be sent in permanent sleep mode until Arduino is reset or we used a timer for specific time for which Arduino goes into sleep mode. This method is dependent upon libraries that might not work on some Arduino boards.
Again, we will take an LED example to demonstrate its working.
Sleep sleep;
unsigned long offTime;
void setup() {
offTime = 5000;
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
delay(1000); // delay of 1 sec
digitalWrite(LED_BUILTIN, LOW); // turn off LED
delay(1000); // 1 sec delay
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(offTime); //sleep for: offTime
}
At the start of the sketch, we have called the Arduino sleep library and defined a variable offTime this variable will store the time limit for which we want to put Arduino in sleep mode. Here we have taken 5000ms of offTime. Then we defined a built-in LED for Arduino.
In the void loop section LED code is written, here LED will complete one cycle of blinking then goes to sleep mode for 5000ms or 5 sec, after that LED will again start one blinking cycle then again goes to sleep mode for 5 sec. This cycle continues until Arduino is reset.
In this example loop will halt for 5 sec after which it starts again because of the offTime set but if we need to stop it permanently we have to remove this timer or set it to maximum value. Sleep library can halt the loop section for a maximum of 49 days after which it will reset automatically.
iii: Using Exit (0) Statement
The void loop can also be stopped using exit() statement. This statement is one of the simplest ways to stop a void loop function. Remember to use this statement at the end of your code because any instruction written after this line will not be executed until Arduino is reset or code is reuploaded.
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
delay(1000); // 1 sec delay
digitalWrite(LED_BUILTIN, LOW); // turn off LED
delay(1000); // 1 sec delay
exit(0);
}
Above code explains the use of exit statements for stopping the void loop in Arduino code. Like previous example LED blink code is written and at the end of code we have initiated an exit line. This will break the Arduino void loop. LED will blink for one cycle after that it will stop.
iv: Using if Statement
The if statement is the last method, we will discuss today to stop the void loop. If-statement will set a condition inside the loop which will either restart the void loop or stop it depending upon user input.
Below code demonstrates if-statement with the help of LED to stop void loop.
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if(stop==true){
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
delay(1000); // 1 sec delay
digitalWrite(LED_BUILTIN, LOW); // turn off LED
delay(1000); // 1 sec delay
}
stop=false;
}
At the start of code, we have initialized a Boolean variable stop and its value is set to true. After that in the setup section LED pins are defined. In the void loop section if statements begin and check for the variable stop value. If the condition becomes true it will start LED blink sketch. Once the LED blink cycle is completed, we assign the value of the stop variable to false. After that it will keep checking the void loop but the statement will always become false because we set the value of the stop variable to false, resulting in halting the void loop of Arduino code.
Conclusion
The void loop is an important part of Arduino code, anything written inside it will keep on running until Arduino is reset. Sometimes we need to stop the loop section according to project parameters so here we highlighted all the major ways using which we can stop Arduino void loop section code.