Saving data on EEPROM detecting power down

Hello every one,

I need to save important data that is changing while programm runs. Just in this moment, I am detecting the power down, using an analog input, and using a battery to let the Arduino Mega 2560 save the information on EEPROM, but I have the problem that if the power down is too long, the battery get down fastly and when the system power up the battery doesn't support the circuit.

Can someone give some idea to get this goal?

Thanks in advance, this is driving me crazy.

Will

The analog way it's better to set the reference to an internal one (by default it's relative to the supplied voltage, and that way it won't work because it's not constant unless a step-up is used), and scale the threshold voltage accordingly (with a fixed divider or a potentiometer).

The digital way would be setting the right values for a voltage divider, that when reaching the threshold the pin reads low. I think the digital reading is also relative to the supply voltage, but in a linear proportion; so it should be possible if the "sweet spot" is found.

In any case, be aware of the brown-out detector; it triggers a reset (the same one as pressing the button) at a supply voltage of approx. 2.8v.

PD: in order to preserve the EEPROM's life, save the data only once (if it happens due to the low voltage alarm, not in normal operation of course), and then either keep running or halt (by a sleep mode or infinite empty loop).
Although in that situation it's better to power down the system for good, letting the brown-out detector (BOD) reset the system may cause some erratic behavior like saving the data multiple times before the actual power-off. That is, unless beforehand it's in an asleep state with the BOD turned off.