Store in flash memory

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 ()

does not sound like a C++ statements... did you miss the // for the comments?


you mention saving in flash. What type of Arduino do you have?

LOL. Yes, I did omit the "//"

For this, I will be using an Arduino mini

EEPROM

you won't be able to write in flash from your Arduino sketch - but the Arduino pro-mini does have an EEPROM

read the EEPROM library documentation and look at the examples;

remember that an EEPROM address can only be written 100,000 times so think a bit about how often you want to save something there.

Thankyou.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.