Pages: [1]   Go Down
Author Topic: A toggle switch to call a function and then turn off the Arduino  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

I would like to know if it's possible that if I press a toggle switch (to turn on/off the Arduino power), to first call a function in the sketch, such as "SaveBeforeShutDown()", and only turn off the Arduino one second after to let the function enough time to finish, or better if possible, turn off immediately when the function has exited.

It must be possible, but how?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You'll need external hardware to control the Power.  The arduino board has no control over its power source.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes that's why I posted here and not in Programming smiley

What external hardware would be required?
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will need a switch to turn off the power. Something like this:

Code:
  if (button_pressed(power_off)) { //power off button pressed
    do_something();
    //wait(duration_1s); //wait for a second
    turn_power_off(); //turn off power
  }

The switch is turned off by a pin, activated by turn_power_off().

A more interesting case is to use one button to turn on / off to the mcu. Here, you need a switch (or a switch-able power source), and a self-locking mechanism (implemented in software).
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A while ago somebody posted a solution which used a capacitor+diode on the power supply to keep the Arduino running for a moment after the power supply was disconnected, and monitored the supply voltage upstream of the cap so that it had advance warning when the power supply was about to fail. It only ran on for a moment, but that was apparently long enough to save its state to EEPROM so that it could be reloadedwhen the power supply was restored.
Logged

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

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This was the first thing that came to my mind when I read the subject.  smiley

http://hackaday.com/2012/12/22/an-even-more-useless-machine/
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are plenty of cases where you want a machine to perform certain tasks and then turn itself off: a coffee maker, an oven, a washer, etc.
Logged

Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are plenty of cases where you want a machine to perform certain tasks and then turn itself off: a coffee maker, an oven, a washer, etc.


My example has 8 channels and uses an ATMega8 to boot!  smiley
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16517
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes that's why I posted here and not in Programming smiley

What external hardware would be required?

Could be as simple as using a single coil 5vdc latching relay to allow a sketch to turn off it's power source.

http://img25.imageshack.us/img25/563/08miq7.jpg
Logged

Pages: [1]   Go Up
Jump to: