how to store int values in variable after power failure or external restart?

hello every one!

i used example of of arduino “statechangedetection” which count how many time button is pushed.
i want to ask that if i press reset button or if there is power failure , the value stored in variable (no of button pushed) set to zero.
how can i hold this value even after reset button is pressed or after power failure.

Thanks,

You can store the value in EEPROM memory but be aware you have a limited number of times you can write to the internal EEPROM memory before it fails. You could maybe implement some wear levelling system to extend the EEPROM life.

how to store int values in variable after power failure or external restart?

Just a small point - the values must be stored BEFORE the power failure.

If the data is important it may be advisable to have a backup-battery that can keep the Arduino running for a while after the power failure. If it detects that the power has failed it can save the data to EEPROM and shut down.

...R

thanks for your replay
i am now thinking to use backup battery with.
is there any option that arduino should run on two power source if one fails it should switch on other by saving all data as usual. i want to power arduino from usb port from battery charger and also connect battery to use if power fails..

thanks

My approach to battery backup for mains power failure is to run the Arduino from a battery that is constantly trickle-charged from the mains. With a diode between the battery and the battery charger the battery cannot feed power back to the charger when the mains fails. Consequently by monitoring the voltage from the battery charger (upstream of the diode) the Arduino can tell if the mains fails.

...R

Don’t worry about wear in the EEPROM unless you plan to push the button over 100,000 times.

When you increment the counter, store the value in EEPROM using EEPROM.put(0, Counter);

In setup(), read the counter from EEPROM using EEPROM.get(0, Counter);

See: Arduino - EEPROM

my counter can go up from 100,000.
is there any way i use battery to hold value if main supply fails?

Thanks

i am now thinking to use backup battery with.

There's your problem...
What happens when the battery goes flat during an extended blackout?

Simplest circuit for POWER LOSS DETECTION
The electrolytic capacitor in the middle can provide enough runtime to save your volatile variables to EEPROM as the cap 'runs down'.

Remember to restore those valriables after power is restored !

lastchancename:
There's your problem...
What happens when the battery goes flat during an extended blackout?

There will probably be a known amount of time during which the battery can supply the Arduino. Note the time when power fails and if it has not been restored when 90% of the on-battery time has elapsed go through an orderly shut-down.

...R

You can still use the same circuit.
Simply put the battery in front of the diode with the primary supply.
Then when supply or battery fall. slow the low-voltage threshold, save your goolies. No loss, no fuss, works both ways.

I use DS1307 extra ram for saving data.It can be battery backed.No wear problem.