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);
}
}
}
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.
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.