How to stop execution of a sketch?

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.

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

or

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

johnwasser:
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.

void stop()
{
  while(1);
}

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

volatile int stopFlag;

void stop()
{
  stopFlag = 1;
  while(stopFlag);
}
// 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 :slight_smile:

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

AlfaOmega:
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 think this is how one would go about putting the CPU into a power-down sleep:

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

void stop()
    {
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    cli();  // Disable interrupts
    sleep_mode();
    }
1 Like

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:

void loop() {

  if( something == true ) {
     performAction();
  }
}

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.

!c

I tried that "put to sleep" thing. now I feel bad for my Snapino!

#include <assert.h>

// And to halt put assert(0);

:mindblow:

Ah dear, ever the "XY Problem". :astonished:

This thread is over 6 years old.

sterretje:
This thread is over 6 years old.

guess that sleep function worked really well.

Use

while(true) {}

that will stop it forever!!

"

Use

while(true) {}

that will stop it forever!!

"

WARNING:

NOT TRIED OUT...

I think however, that we can be reasonably confident. :grinning:

(Add a semicolon.)

I tried

for (;:wink:
;

on an esp8266 with Arduino IDE , and it did a
WDT reset dump the stack to the serial monitor
and hung .....

Go figure.

I did edit eventually to Serial.print in a forever loop .

on an esp8266 with Arduino IDE , and it did a
WDT reset dump the stack to the serial monitor
and hung .....

Go figure.

Yes the loop function needs to exit to kick the dog. You don't have to do this on real Arduinos.

Notice that winkie smiley in your code? Did you put it in there?
This is the forum mangling code and mistaking it for format information. This is why we are so hot on posting you code correctly.

I'm sorry . I did screw up by not using code tags.
Forgive me.