Go Down

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

AlfaOmega

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?
_

johnwasser

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() {}


or

Code: [Select]

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AlfaOmega

#2
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 ))
_

robtillaart

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()
{
 while(1);
}


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

Code: [Select]
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 :)

Rob Tillaart

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

Grumpy_Mike

Quote
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.

pYro_65

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

PeterH


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.

johnwasser

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()
    {
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    cli();  // Disable interrupts
    sleep_mode();
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

drone

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 ) {
     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

Go Up