I have code for a toggle switch. In the event of a power failure, I would like to store the LEDs last state in flash memory so I can restore it when power come back.
I have not done this before.
Please guide me.
Here is my code:
int LEDState = 0;
int LEDPin = 8;
int buttonPin = 12;
int buttonNew;
int buttonOld = 1;
int dt = 100;
void setup()
{
pinMode (LEDPin, OUTPUT);
PinMode (buttonPin, INPUT);
Serial.begin (9600);
} End setup ()
void loop()
{
buttonNew = digitalRead (buttonPin);
if (buttonOld == 0 && buttonNew == 1)
{
if (LEDState == 0)
{
digitalWrite (LEDPin, HIGH);
LEDState = 1;
} End if
else
{
digitalWrite (LEDPin, LOW);
LEDState = 0;
} End else
} End if
buttonOld = buttonNew;
delay (dt);
} End loop ()