Shutdown arduino + other misc questions

  1. Is there a function to shut down the board?

  2. Can setup() be called in other functions?

  3. What if a return; statement is inserted into the loop() function?

  1. There is a sleep function, but it does not shut it down entirely. If you want it completely off, I suggest an on/off switch.
    Arduino Playground - ArduinoSleepCode

  2. Not sure, but it would seem like that this is not acceptable, but again, not sure.

  3. Return, returns the value of an function.
    http://arduino.cc/en/Reference/Return

Technically there's no reason you can't call setup() - but there's probably not any good reason to. If your setup does things that should only be done once at program start (like Serial.begin()), then it would be a really bad idea.

Putting a "return" in the loop() function won't do anything. The actual code that's generated by the IDE calls the loop() function in an infinite loop.

Here's the final coded generated for the "Blink" example program:

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

#include "WProgram.h"
void setup();
void loop();
int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

The "real" program that runs is the main() function. Note that it calls some Arduino core initialization that's not visible to the program (init()), calls your set() function, and then loops forever calling loop().

So there's no way to make the program "end". Besides, what would be the point of having a program end on a micorcontroller? If that happened the chip would just go "dead" until it got reset.

If you really want this behavior you can simply comment out the for loop in the main program template so it only calls loop() once (or change it to do something more radical like not call loop() at all):

hardware/cores/arduino/main.cxx

There's no way to terminate the infinite loop that keeps on calling "loop ()", over and over again. But if you want the Arduino to stop what it's doing and "hang", you can code your own infinite loop, doing nothing:

for(;;)
   ;

Once the flow of control enters that loop, it'll never exit. The Arduino continues to run, though, until reset. This means that it will continue to draw power from your power supply, which is bad if it's a battery! In that case, use the "sleep" technique mentioned above.

Thanks for all the replies.

Any suggestions as to shutting down the board through a software trigger? Transistors maybe?

It depends on what you mean by "shutting down the board". Do you want to just stop the actions of your sketch? Or do you want to stop things like PWM outputs and servo pulses, too? Or are you trying to switch off the board to reduce power consumption?

I'm trying to shutdown all powered components.

Example scenario:
Temperature logger which stores readings taken every 30 seconds into EEPROM. I would like to shut down the entire system after data has been written to the final memory location.

Putting a "return" in the loop() function won't do anything.

To correct this slightly-- the return statement does do something. It skips any and all later work that is listed in the current function.

void loop()
{
    if (digitalRead(2) == LOW)
        return;
    Serial.println("Pin 2 is HIGH.");
    delay(1000);
}

The prints won't happen as long as pin two is held LOW. If it goes high, it starts printing once per second, and stops when the pin goes low again.

As someone else pointed out, the real code that you don't see calls loop() forever. That code looks like this:

void main()
{
    init();
    setup();
    while (1)
        loop();
}

The while (1) { ... } runs forever. Any early return from the loop() function will just cause another loop() call to commence immediately.

Temperature logger which stores readings taken every 30 seconds into EEPROM. I would like to shut down the entire system after data has been written to the final memory location.

You can easily stop writing when you hit the end. Even if you don't find a way to cut the power, you should do something like this:

#define CONFIG_LOG_BEGIN = 0x00
#define CONFIG_LOG_END = 0x1FF // depends on ATmega model

int address = CONFIG_LOG_BEGIN;

void loop()
{
    // ...
    long reading = my_collect_a_reading_function();
    if (address+sizeof(reading) <= CONFIG_LOG_END)
        address += EEPROM_writeAnything(address, reading);
    // ...
}

The "reading" might be some other type of data type or structure you find useful. For the EEPROM_writeAnything() routine, see Arduino Playground - HomePage .

Thanks for all the response halley, but I've already found the final memory address and know how to stop writing.

I am still looking for a way to shut the system down though.

OK, so do you want to shut down and then later, restart? Because if so, you'll probably be best using the sleep functions of the AVR chip. Not sure how that works at the Arduino/C++ level.

No. Currently just shutting down will be fine.

Side note: why doesn't this forum have a quote feature?

No. Currently just shutting down will be fine.

Side note: why doesn't this forum have a quote feature?

It has, it' just not configured to show it :frowning:

Copy url for 'reply' and insert quote=REPLYNUM;

This is the URL I used to quote you:

arduino.cc/cgi-bin/yabb2/YaBB.pl?action=post;num=1237993842;**quote=11;**title=PostReply

Side note: why doesn't this forum have a quote feature?

It's the button just to the right of the "hash" button (#) in the toolbar above the text box.

Oh, and if you don't need to switch the Arduino on again after it has switched itself off, then a MOSFET in the power supply should do it.

Ran across this last night. give it a look. It is uses the sleep function, and there is some source code to look at too. http://interface.khm.de/index.php/labor/experimente/sleep_watchdog_battery/
He breaks down the Arduino to the core needs. Minimalism at it's best.

Would this work:

Those are npn transistors. they're not oriented the right way, i realize.
The resistor represents the arduino.

The switch is a momentary power switch. The logic input (shown low in the image) is driven by the micro controller. The user must press the power button for as long as it takes the controller to set that the logic input to high (as soon as this input turns on, an indicator will turn on). The user may now let go of the power button, at which time current will still be supplied as one of the transistors is on. Somewhere in the code, I can set this logic input to low when i want the system to shutdown.

It works in this simulator: http://www.falstad.com/circuit/

Import this circuit if you want to simulate.

$ 1 5.0E-6 10.20027730826997 50 5.0 50
R 256 304 256 336 0 0 40.0 5.0 0.0 0.0 0.5
w 448 128 448 288 0
g 448 320 448 336 0
w 448 288 448 320 0
s 176 304 176 224 0 1 false
w 224 224 176 224 0
w 256 304 176 304 0
w 256 128 256 208 0
w 256 240 256 304 0
t 336 224 304 224 0 1 0.6698509496766558 0.0 100.0
w 256 304 304 304 0
w 304 304 304 240 0
L 368 224 400 224 0 1 false 5.0 0.0
w 336 224 368 224 0
w 304 208 304 128 0
r 304 128 448 128 0 100.0
w 304 128 256 128 0
t 224 224 256 224 0 1 8.881784197001252E-16 -0.6698509496766549 100.0

Oh, and if you don't need to switch the Arduino on again after it has switched itself off, then a MOSFET in the power supply should do it.

I don't have any experience with MOSFETS, do they work the same way as bjt's except with voltage?

Have you considered a solution with a small 5V relay, powered as long as a digital output is high?
Once you set that out pin to low, the relay will disengage and disconnect the power from the board.

Have you considered a solution with a small 5V relay, powered as long as a digital output is high?
Once you set that out pin to low, the relay will disengage and disconnect the power from the board.

Yes. See my previous post. I am using bjt transistors instead of relays. I built the above circuit last night and tested it using 2 switches instead of 1 plus the digital logic. It worked fine.

Thanks for all the help everyone.

do we need 2 transistors?

have both the arduino out pin and the momentary on button driving the same transistor's base.

also, a capacitor on the momentary on button could minimise the button down time to successfully start the arduino.

void setup
{
private_setup();
}

void loop
{
// do something
if (need_to_call_setup)
private_setup();
}

void private_setup
{
// all the necessary setup stuff
}