Go Down

Topic: How to stop execution of a sketch? (Read 2700 times) previous topic - next topic


Hi all!
This is a general question, not related to a particular project.

How execution of a sketch can be stopped?

One option I can immediately think of is to simply make a for() loop in the
setup() part of the sketch. That is to say blink a LED 10 times.

What other ways are there?
How about stopping the code in the loop() part?


If you want to get really fancy you can put the Arduino into a power-down sleep but usually you just put it in a loop.

Code: [Select]

void setup() {for(int i=0;i<10;i++) blink;}
void loop() {}


Code: [Select]

void setup() {}
void loop() {for(int i=0;i<10;i++) blink; while(1);}

Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Jan 11, 2012, 04:01 pm Last Edit: Jan 11, 2012, 04:09 pm by AlfaOmega Reason: 1

put the Arduino into a power-down sleep

Cool! That's what makes a pro stand out from a crowd of amateurs ))


johnWasser is right, an endless loop will "stop" it. [Stopping means preventing executing other code here] You could put of course that into a function.

Code: [Select]
void stop()

And an some advanced version could make the stop conditional ..

Code: [Select]
volatile int stopFlag;

void stop()
 stopFlag = 1;
// and have some Hardware IRQ (attachInterrupt or timerinterrupts etc) to reset the stopFlag
void IRQ()
 stopFlag = 0;

Another way to stop a sketch is to have a relay between your power supply and the Arduino. Then a digitalWrite(RELAYPIN, LOW) could shut of the system ...
Disclaimer: never tried :)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


How execution of a sketch can be stopped?

Essentially you can't, you can only do stuff that has the side effect of appearing that it has stopped.

The simplest way it to put everything in the setup() function and have a blank loop() function.


Hehe, you could setup a fancy little circuit so when you are done running your sketch, a transistor switch can put some dangerously high voltage into the arduino
Forum Mod anyone?


How execution of a sketch can be stopped?

To really stop it, turn off the power.

To simply stop the sketch from doing anything after some point, code it so it stops doing anything after that point.
I only provide help via the forum - please do not contact me for private consultancy.


I think this is how one would go about putting the CPU into a power-down sleep:

Code: [Select]

#include <avr/interrupt.h>
#include <avr/sleep.h>

void stop()
    cli();  // Disable interrupts
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


It's worth noting, as mentioned by a few above, what does "stop" mean?

Does it mean - stop taking some prescribed action and wait until some signal occurs to resume activity, or does it mean that the sketch should perform a single activity, and then do nothing else until it has been power-cycled or reset, or, even more - does it mean the device should stop running entirely after performing some action?

For 3rd, you will need to control the state of the chip's sleep mode, as people have mentioned.

For the 2nd, as noted - simply put all of the logic in setup() and have a blank loop.

For the 1st, consider using a simple state machine:

Code: [Select]

void loop() {

  if( something == true ) {

Of course, your state machine will be arbitrarily complex based on your actual needs - but it is important to differentiate between the three possible outcomes as the solution to each is different.


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131