How to retain the previous button state even after switching off the Arduino

I have programmed a WS 2812 LED for three different colors, by pressing a switch.

  1. first press the blue light glows,
  2. second press the yellow light glows for some time and changes to red.

now when I restart the Arduino mini the sketch starts from the first. where as I would want to remember the last state and then continue the sketch from the previously left state.

for example when I switch off the Arduino mini the state is yellow light then after switching on the sketch should continue from the yellow light and not start from the first Blue light.

please advice how to do this.

This is what the EEPROM is for.

Research it.

but EPROM has only 100000 rewrite limit is what, I have read about. If so that will be very limited for my use case.

can you suggest any other method.

When you switch off the Arduino, save the switch setting to EEPROM.
Search power fail detection.

That’s at least 100,000 power cycles.

You could save the number of power cycles to EEPROM, when you approach 100,000 then start writing at other locations, etc.

can you suggest any other method.

Use an external FRAM chip.

sunilj:
but EPROM has only 100000 rewrite limit is what, I have read about. If so that will be very limited for my use case.

OK, now just who is going to press your switch 100,000 times?

If you change address each time you push a button and leave the previously used address at 0XFF then you get into 102,400,000 times before it could potentially go wrong.

Which at one press a second 24 hours a day will give you a life expectancy of 3.29 years.

I suspect the button will fail first.

Grumpy_Mike:
If you change address each time you push a button and leave the previously used address at 0XFF

Mike, isn't the EEPROM organised into pages (64 bytes or something?) and it's the pages that wear out? This might last only 16 X 100,000 rather than 1024 X 100,000 writes. But that is still more than the OP will ever need. Your suggested approach will spread the wear evenly between the pages.

Another alternative would be an external EEPROM chip such as 22c02b. They have 1,000,000 write cycle lifetime.

Actually, it is much easier - and cheaper that all that.

Just add a DS1307 Real Time Clock module with a coin cell and use its battery-backed RAM. You can re-write that as often as you like.

Needless to say, you will no doubt find a convenient use for the RTC as well. :grinning:

isn't the EEPROM organised into pages (64 bytes or something?) and it's the pages that wear out?

No, not the internal EEPROM. Anyway it is always the individual cells that fail, the fail occurs on the erase cycle not the write cycle.

If you are clever you can write individual bits ( zeros only ) into an address location and it only generates an erase cycle when you try and write a bit pattern that has some bits that need to be a one and there is a zero already stored in it at the same bit. An erase cycle writs all ones into a location. Note you can't do this with the standard EEPROM libiary code.

Grumpy_Mike:
No, not the internal EEPROM

Thanks Mike!

Of corse the eeprom is limited, but just in writing. So you can write it only if the button state change. After, if it appends too much times you can add another botton to press sometimes and if the button added is pressed you, only in this case, write on the eeprom. Of corse to do so you wave to know approssimatly when the power goes off.
More, I don't know if the likit is on all EEPROM or for each point (like, the eeprom has 100 bytes, you can write 2000 times for byte, so you can use it 100*2000=200000 times).

Use EEPPROM.update(cell, value)

It only updates the cell so the lifetime of a cell is increased.

I don't know if the likit is on all EEPROM

No the limit is on individual byte addresses. Why do you not know this as I explained it in my previous answers?

i would just use the Eeprom as if it had an unlimited life every time you pressed the button. 100,000 writes is an awful lot of button presses.

You'll probably wear out the arduino or at the very least the button before the Eeprom fails

I do wish people would read the thread that they are commenting on and not repeat advice already given.

Paul__B:
OK, now just who is going to press your switch 100,000 times?

this is to be connected a rotary system for every rotation there will be one click on the switch and Two entries in to the EPPROM, and the rotation is 20 RPM so 20 x 2 writing in to EEPROM.

sunilj:
this is to be connected a rotary system for every rotation there will be one click on the switch and Two entries in to the EPPROM, and the rotation is 20 RPM so 20 x 2 writing in to EEPROM.

Wow - that is seriously weird! We would love to know why you wish to do this. :astonished:

In any case, the answer with unlimited writing is what I cited in reply #9.

FRAM, trillion and more write cycles. Fast SPI acces speed, and nonvolatile storage (no battery)

MB85RS1MT is a FRAM (Ferroelectric Random Access Memory) chip in a configuration of 131,072 words × 8 bits, using the ferroelectric process and silicon gate CMOS process technologies for forming the nonvolatile memory cells. MB85RS1MT adopts the Serial Peripheral Interface (SPI). The MB85RS1MT is able to retain data without using a back-up battery, as is needed for SRAM. The memory cells used in the MB85RS1MT can be used for 10^13 read/write operations, which is a significant improvement over the number of read and write operations supported by Flash memory and E2PROM. MB85RS1MT does not take long time to write data like Flash memories or E2PROM, and MB85RS1MT takes no wait time.

10^13 = 10,000,000,000,000 10 Trillion!

Plenty of Surface mount parts available also. DIP getting harder to find.