How to remember led states while power off?

Hello guys, I'm new into all of these and tried to control 3 LEDs with an IR remote control. I've managed to do that but i need to add another function to my circuit.

I want to remember my LED states when the power is off.

Found a few samples but couldn't understand them all. If someone can show it on my coding, it'd be highly appreciated.

I'm doing this for a small project which will be graded as a Visa in university. So don't blame me because of i'm asking a direct help.

After this is done only thing i'll need to do is to figure out how to make it work stand alone.

Thanks in advance.

/* 
Microprocessors FIZ434E - Celal Ege Demir 090060127 - Onur Erbay 090090184

*/
 
#include <IRremote.h>
void ledleri_yak(int led_deger); 
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero) 
the first zero must remain zero but you can 
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1  11535 // code received from button 2
#define code2  62569 // code received from button 3
#define code3  56187 // code received from button 4
#define code4  10947 // code received from button 1
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
void setup()
{
  Serial.begin(9600);   
  irrecv.enableIRIn();  // Start the receiver
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}
 
void loop() {
  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch(value) {
       case code1:
         if(itsONled[1] == 1) {        // if first led is on then
            digitalWrite(led1, LOW);   // turn it off when button is pressed
            itsONled[1] = 0;           // and set its state as off
         } else {                      // else if first led is off
             digitalWrite(led1, HIGH); // turn it on when the button is pressed
             itsONled[1] = 1;          // and set its state as on
         }
          break; 
       case code2:
         if(itsONled[2] == 1) {
            digitalWrite(led2, LOW);
            itsONled[2] = 0;
         } else {
             digitalWrite(led2, HIGH);
             itsONled[2] = 1;
         }
          break;
       case code3:
         if(itsONled[3] == 1) {
            digitalWrite(led3, LOW);
            itsONled[3] = 0;
         } else {
             digitalWrite(led3, HIGH);
             itsONled[3] = 1;
         }
          break;          
       case code4:
         digitalWrite(led1, LOW);
             itsONled[1] = 0;
         digitalWrite(led2, LOW);
             itsONled[2] = 0;
         digitalWrite(led3, LOW);
             itsONled[3] = 0;   
          
    }
    Serial.println(value); 
    irrecv.resume();
  }
}

You can store the last state of the LEDs in the Arduino's EEPROM, and read back the state from there at power-up.

Details of how to read to and write from the EEPROM are given here.

You should be careful in your sketch not to write to the eeprom too frequently or unnecessarily. It has a limited number of writes before it wears out and fails, around 100,000. That might sound like a lot, but with the speed that Arduino sketch runs, it could be used up in minutes or hours by a carelessly written sketch.

Paul

PaulRB:
You should be careful in your sketch not to write to the eeprom too frequently or unnecessarily.

I'd recommend some kind of timing thing, probably based on our old favourite, Blink Without Delay. If (say) 5 minutes or whatever time you deem appropriate has passed, write the values.

But if it's crucial that the state is up-to-the-moment, you will likely run out of write cycles. How often do you change the state?

Can you guarantee an orderly shutdown?- in which can you could have some kind of write-on-demand approach with one of the remote's buttons, activated just before power down. Of course, that won't cater for power failure.

How crucial is it that the leds are in the correct state at the next power up?

Honestly, i'll demonstrate the circuit to professor and close it.

Purpose of this is to show the usage of EEPROM.

I can roughly say that, i'll push the IR Remote Control's buttons a few times and that plug out UNO and show that it remembers the latest states.

Then i can re-write a blink code to avoid further failures.

Is there a way to make it write the values to EEPROM in each 1 minute or so?

egedemir:
Is there a way to make it write the values to EEPROM in each 1 minute or so?

Yep- have a look at the millis() command. Every x milliseconds, do a write to eeprom.

See Blink Without Delay

I would write it to EEPROM every time it changes state. Even if you run the thing for a day pressing the button every second you will still not exceed the write limit.

Any help possible on coding? I'm kind of lost trying all things you've said.

I agree with Grumpy_Mike. Doing the math made me think so, i will only demonstrate it bunch of times and it would be enough.

The point is, you only need to save the LED state when you change it by pressing the button.

Now if of course, the LED state was "flashing", you would save the LED state as precisely that - not "on", not "off", but a defined state of "flashing".