EPROM write/read to remember my LED state

hey all im new to arudino.
I started doing my first program and what i want to do is to remember my setings when i power off. Well I set LED state over Serrial port for LED to be ON or OFF (1 is ON, everything else OFF)

Then when board looses power i want ot to restore LED state just as it was before we powered down.

I grouped coede shown below but it does not work it says variable MODE is not declared in SETUP :frowning:

Any help? TNX!

//Following class will facilitate saving data to EEPROM
//[aurdino URL]/playground/Code/EEPROMWriteAnything
#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;
}
// END EEPROM Fasilitator class



// *** Declarations ***
//used to store what we receive
int incomingData = 0;
//the pin on which you connect the anode (+) of the LED
byte ledPin =  13;


struct config_t
{
    long alarm;
    int mode;
} configuration;

void setup()
{
    //open the serial port and set data rate to 9600 bps
    Serial.begin(9600); 
    
    pinMode(ledPin, OUTPUT); 
    //send our status
    Serial.println("Program started.");
    Serial.print("The LED connected to GND and PIN ");
    Serial.print(ledPin, DEC);
    Serial.println(" will be on if you send '1' or off if you send anything else!");

  
    //read eprom
    EEPROM_readAnything(0, configuration);
    // ...
    
    //set initial LED value to what ever we had in the mode variable
    if(mode==1){
      //detected that previous state is 1 == t=turn on LED
      digitalWrite(ledPin, HIGH);    
    }
void loop()
{
  
  //look if we have data
  if (Serial.available() > 0) {
      //get the sent data
      incomingData = Serial.read();
      Serial.print("We received : ");
      Serial.println(incomingData, BYTE);
      Serial.print("The LED is : ");
      //49 = ASCII code for '1'
      if (incomingData == 49){ 
        digitalWrite(ledPin, HIGH);
        Serial.println("ON");
        
        //save eprom value
        mode = 1;
        EEPROM_writeAnything(0, configuration); 
        
        
      } else {
        digitalWrite(ledPin, LOW);
        Serial.println("OFF");
        
         //save eprom value
        mode = 0;
        EEPROM_writeAnything(0, configuration); 
        
      }
      Serial.println();
  }  
  
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...

    // if they push the "Save" button, save their configuration
    /*if (digitalRead(13) == HIGH)
        EEPROM_writeAnything(0, configuration);
      }
    */
}

Try this...

    if([glow]configuration.[/glow]mode==1){

tnx for reply!
i chanfged mode to configuration.mode but now im geting new build error :frowning:

error is:
In function ‚Äėvoid setup()‚Äô:
error: a function-definition is not allowed here before ‚Äė{‚Äô token

below is updated code. Hope u can help me out! TNX!

//Following class will facilitate saving data to EEPROM
//http://www.arduino.cc/playground/Code/EEPROMWriteAnything
#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;
}
// END EEPROM Fasilitator class



// *** Declarations ***
//used to store what we receive
int incomingData = 0;
//the pin on which you connect the anode (+) of the LED
byte ledPin =  13;


struct config_t
{
    long alarm;
    int mode;
} configuration;

void setup()
{
    //open the serial port and set data rate to 9600 bps
    Serial.begin(9600); 
    
    pinMode(ledPin, OUTPUT); 
    //send our status
    Serial.println("Program started.");
    Serial.print("The LED connected to GND and PIN ");
    Serial.print(ledPin, DEC);
    Serial.println(" will be on if you send '1' or off if you send anything else!");

  
    //read eprom
    EEPROM_readAnything(0, configuration);
    // ...
    
    //set initial LED value to what ever we had in the mode variable
    if(configuration.mode==1){
      //detected that previous state is 1 == t=turn on LED
      digitalWrite(ledPin, HIGH);    
    }
void loop()
{
  
  //look if we have data
  if (Serial.available() > 0) {
      //get the sent data
      incomingData = Serial.read();
      Serial.print("We received : ");
      Serial.println(incomingData, BYTE);
      Serial.print("The LED is : ");
      //49 = ASCII code for '1'
      if (incomingData == 49){ 
        digitalWrite(ledPin, HIGH);
        Serial.println("ON");
        
        //save eprom value
       configuration.mode = 1;
        EEPROM_writeAnything(0, configuration); 
        
        
      } else {
        digitalWrite(ledPin, LOW);
        Serial.println("OFF");
        
         //save eprom value
        mode = 0;
        EEPROM_writeAnything(0, configuration); 
        
      }
      Serial.println();
  }  
  
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...

    // if they push the "Save" button, save their configuration
    /*if (digitalRead(13) == HIGH)
        EEPROM_writeAnything(0, configuration);
      }
    */
}
      digitalWrite(ledPin, HIGH);
    }
[glow]}  // <---[/glow]

void loop()
{

yey im so unatebtive :slight_smile:
thanks so much!

Now theoretical question:

How many times can i re-wrire variables in EPROPM of ATMEGA-328P before it starts to fail? (well thats some what hardware questin) but its directly related to what im doing with thi soft.

Im doing this project to controll lighhts over PC. So when PC is on we are good but when PC is off we want to remember previous light settings. THe code above saves lights status at any time user clicks the button (on software) so really we can have 1 variable to be re-written abou 10-20 times per day. So im really wondering how relibale this is?

TNX!

I've read the EEPROM is good for 100000 writes (per byte of EEPROM, I assume). One idea (for how to improve this) is to do a read before any write and skip the write if the intended value is already there.

ok 100k revrites will give me about 13 years of operation :slight_smile:
Not too much but I can live with that...

Also tnx for advice on checking existing stus code before writing new one... that might help in some cases :slight_smile: