Go Down

Topic: Shutdown arduino + other misc questions (Read 5 times) previous topic - next topic

FusiveResonance

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?

MYX

#1
Mar 25, 2009, 08:19 pm Last Edit: Mar 25, 2009, 08:19 pm by MYX Reason: 1
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.
http://www.arduino.cc/playground/Learning/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

etracer

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:

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

Anachrocomputer

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:

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

FusiveResonance

Thanks for all the replies.

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

Anachrocomputer

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?

FusiveResonance

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.

halley

#7
Mar 26, 2009, 05:57 pm Last Edit: Mar 26, 2009, 05:58 pm by halley Reason: 1
Quote
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.

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

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

halley

#8
Mar 26, 2009, 06:05 pm Last Edit: Mar 26, 2009, 06:05 pm by halley Reason: 1
Quote
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:

Code: [Select]
#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 http://arduino.cc/playground/Code/EEPROMwriteAnything .

FusiveResonance

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.

Anachrocomputer

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.

FusiveResonance

No. Currently just shutting down will be fine.

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

AlphaBeta

#12
Mar 27, 2009, 03:55 am Last Edit: Mar 27, 2009, 03:57 am by AlphaBeta Reason: 1
Quote
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 :(

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

This is the URL I used to quote you:
Quote
arduino.cc/cgi-bin/yabb2/YaBB.pl?action=post;num=1237993842;quote=11;title=PostReply


Anachrocomputer

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

MYX

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.

Go Up