Save Data before Shutdown I am building a positioner and I store the current position to EEPROM when the move is complete. On start up, I want the positioner to return to the HOME position. I am running an UNO powered from the USB.
My problem is that if the Arduino is turned off, I would like to stop the move and store the current position before the Arduino dies.

Note that this would also be relevant to devices like thermostats which remember the set point through a power outage.

POSSIBLE SOLUTIONS


Store position in EEPROM at each step. No, that will wear out the EEPROM.
Connect a large capacitor from +5V to Gnd. When this falls below a set level, trigger an interrupt (ISR) that stops motion and stores current position in EEPROM.


QUESTIONS


Will #2 work, and how could I program it?
Any other ideas I am missing?
Thermostats would save the set point to eeprom once when the set point it set by the user. On startup it would just read the set point.

The analogue pins measure relative to VCC. So if you short the analogue pint to VCC the result will remain at max even as the voltage drops.

Instead use a diode to separate the measurement point from the power supply.







simulate this circuit &ndash; Schematic created using CircuitLab

As long as power is supplied the gpio input will be high. As soon as the power is removed the gpio pin will be low and the capacitor will need hold enough charge to save the position.

However what would happen if the positioner is manipulated when the arduino is off? Most devices have a callibration routine using end-of-travel detection or something similar they run on startup to find the home position.