Pages: [1] 2   Go Down
Author Topic: Shutdown arduino + other misc questions  (Read 3939 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Duinian Citizen
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 25, 2009, 02:19:55 pm by MYX » Logged

Indiana
Offline Offline
Full Member
***
Karma: 1
Posts: 234
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 * 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
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all the replies.

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

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
« Last Edit: March 26, 2009, 11:58:40 am by halley » Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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 .
« Last Edit: March 26, 2009, 12:05:38 pm by halley » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No. Currently just shutting down will be fine.

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

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
« Last Edit: March 26, 2009, 09:57:14 pm by AlphaBeta » Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
Duinian Citizen
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: