How to store previously chosen RGB LED color and light up after reboot


Beginner hobbyist here. I am building an RGB LED controller (using LED SMD 5050) that takes inputs from an IR remote. I found a code that does that and have understood the basics of it (credits to author).

However I would like to modify it so the Nano stores whatever color choice user makes (via remote) and later if the whole unit (Arduino + LED strip) is power cycled, to restart with color stored in memory. Can someone please walk me through the steps or logic to do this (as beginner friendly as possible, since I'm a slow learner)?

I feel I should start with line 77 and modify the Random color variable. But don't know how to proceed beyond that.

I tried posting the code here but looks like I'm exceeding the post length of 9000 caharcters. So sharing Github link:

Take a look at the EEPROM library in the Arduino reference.

Things to watch out for are:
Don’t try too many write to the EEPROM - it had limited erase/write cycles.

Perfect, that is indeed what I needed to hear, thanks.

I 've spent the past few days trying to understand EEPROM read/write and incorporate it. However, I’m running into a wall since the variable I’m trying to store is a 3 element array. How do I store/retrieve a 1-D array into the EEPROM? All tutorials online talk about 2D arrays. I’m a complete beginner and am unable to digest how it works for an array. Should I put every read/write in a for loop? I would appreciate someone sharing an example. Also, I did try writing a for loop to read and one to write and was wondering if that’s inefficient. Clearly I can do with y’all’s help and advise.

ex: int red[3] = {255, 0, 0}

But per my understanding of EEPROM write, this is what I did to store red[3] in it:

int addr = 0;
int stored_color

//when user presses remote button for red:
stored_color= EEPROM.write(addr, red)

//In void loop, retrieve the stored color:

I realize after debugging that clearly this method is wrong since I see changing numbers being displayed for the same color (ex: on serial monitor, red once shows up as 78, and then as 346 and so on).

So would this be the right method to read/write a 1D array?

for (addr=0; addr<3; addr++) {
stored_color = EEPROM.write(addr, red[addr])

and the same for a read version?

Have you looked at the EEPROM library’s get and put methods?

I had not, but now I did. So if I'm understanding it right, my initial attempt needs to be changed to include the number of elements in the read operation?


int addr = 0;
int stored_color

//when user presses remote button for red:
stored_color= EEPROM.write(addr, red)

//In void loop, retrieve the stored color:
stored_color= EEPROM.get(addr, red)
serial.print (red, 3) // 3 being the addition to be made

Did I get this right?
but what about addr - since it was storing 3 bytes, wouldn't it have incremented to 2 or 3 after the write operation? (0 ->1->2 for the 3 bytes in red[3]). Consequently would I have to reset addr to 0 just before running the read/get command?

The way I had been visualizing it so far was different. I thought when we write/read anything to EEPROM that is longer than a byte, it gets lost. and thus one should either manually increment the address pointer for every additional byte that is being written/read or explicitly let the EEPROM read/write command know what kind (or how big) of data is coming in.

But with the new understanding, it looks like eeprom.write (or even put) understands before hand what the data type is and correspondingly stores it no matter how many bytes are required. It is just that during print, we need to let it know how many bytes we want to retrieve..... rt?

I will go test this out later today with my Nano.

If you’re going to use get, it would be more consistent to use put too, and not write.

If you anticipate a tremendous number of stores to EEPROM there are libraries you can use to enforce 'wear leveling' - spreading out the writes over the whole EEPROM so as not to abuse one particular location.