3 lights power recovery

hey i'm working on a project where i have 3 lights and a switch and i have it working now when i press that button several times it goes to the other light but when i start my arduino on new start it starts again at light I actually want him to remember where it was before I restarted and that he goes back to that for example that I have it on light 2 and then off and in my arduino and that light 2 lights up again someone who know how i can make this via EEPROM or something

CODE,

int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int switchPin = 4;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

void setup() {
  
  digitalWrite(ledPin1, HIGH);
  pinMode(switchPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  count = 0;

//debounce function to stabilise the button
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;  
}
void loop() {
  lastButton = currentButton;
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (count == 0) 
    { 
      count++;
      digitalWrite(ledPin1, LOW); 
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
    }

    else if (count == 1)
    { 
      count++;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH); 
    }

    else if (count == 2)
    {
      count = 0;
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW); 
    }
    }
  }

Then don't turn the Arduino off. :roll_eyes:

Or you can arrange a second button which you can press to save the current count to the EEPROM before you turn it off, and next time it starts, you read the EEPROM. :+1:

Where is the } at the end of setup()? What you posted will not compile.

@noedl Care must be taken when using EEPROM as a beginner. EEPROM has a limited number of write cycles for its lifetime. If coded incorrectly, your sketch could use up the life of the EEPROM very quickly and the EEPROM could become useless. I recommend you to attempt to write your code to use the EEPROM but post it here for us to review before you upload it to the Arduino.

Note that I cautiously did not suggest any form of repetitively writing to the EEPROM, but only when a button was pressed. :grin:

You can design a circuit to detect when power is lost to the uC and store enough in a capacitor to keep it running to store your settings.

Hello noedl

Try this modified sketch:

#include <EEPROM.h>
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;

// EEPROM addresses
int ledAdress1 = 0;
int ledAdress2 = 1;
int ledAdress3 = 2;

int switchPin = 4;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

void setup() {

  digitalWrite(ledPin1, HIGH);
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  count = 0;
  digitalWrite(ledPin1, EEPROM.read(ledAdress1));
  digitalWrite(ledPin2, EEPROM.read(ledAdress2));
  digitalWrite(ledPin3, EEPROM.read(ledAdress3));

}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}
/*
 * Note: An EEPROM write takes 3.3 ms to complete. The EEPROM memory has a specified life of 100,000 write/erase cycles, so using this function instead of write() can save cycles if the written data does not change often
 */
void upDateEEPROM()
{
  EEPROM.update(ledAdress1, digitalRead(ledPin1));
  EEPROM.update(ledAdress2,  digitalRead(ledPin2));
  EEPROM.update(ledAdress3,  digitalRead(ledPin3));
}

void loop() {
  lastButton = currentButton;
  currentButton = debounce(lastButton);
  if (lastButton == false && currentButton == true)
  {
    if (count == 0)
    {
      count++;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
      upDateEEPROM();
    }

    else if (count == 1)
    {
      count++;
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH);
      upDateEEPROM();
    }

    else if (count == 2)
    {
      count = 0;
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      upDateEEPROM();
    }
  }
}

Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP