Pages: [1]   Go Down
Author Topic: How to stop execution of a sketch?  (Read 976 times)
0 Members and 1 Guest are viewing this topic.
Antarctica
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8667
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {for(int i=0;i<10;i++) blink;}
void loop() {}

or

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

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

Antarctica
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

put the Arduino into a power-down sleep

Cool! That's what makes a pro stand out from a crowd of amateurs ))
_
« Last Edit: January 11, 2012, 10:09:40 am by AlfaOmega » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
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 smiley

Logged

Rob Tillaart

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33304
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8667
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 323
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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
Logged

Pages: [1]   Go Up
Jump to: