EEPROM help for a noob

Hi All,

First time post so please be gentle!

My program has a DS1307 clock and at a set time it will ramp up some LEDs to a set percentage and then at a set time will gradually lower the LEDs to 0% I have a menu set up which can adjust the on/off times and the max brightness of each LED string what I would like to do is to save any adjustments in to the EEPROM but I understand the EEPROM only has a certain amount of write/erase cycles so I only want it to write it if the value changes, I have searched but everything looks complicated is their an idiots guide anywhere?

At the moment the value is stored like this :-
int weekOn = 15;
int weekOff = 22;

This turns the lights on at 3pm and off at 10pm so the value should only between 0 and 23 so can I check to see if the value has changed and then write it to the EEPROM inside the loop somewhere and then in the setup read the value?

Thanks in advance,
Gavin

You could put the EEPROM write code in the "menu set up which can adjust the on/off times and the max brightness of each LED string". Just set a flag when someone enters data through the menu set up and when you exit the menu you can write to EEPROM if the flag is set. Clear the flag when you read the EEPROM or update the EEPROM. EASY! :slight_smile:

What John said +

You also have some battery backed RAM (if you have the clock coin battery installed) in the DS1307 that can store data. This is accessible throught the I2C interface. Same deal - you probably don't want to rewrite the data every time, but it is an alternative to the EEPROM. See my code repository (link below) for my library that allows access to this memeory if your current version does not.

Thank you both for your help!

I was wondering if I could use the clock as a backup I will check out the code in a while XD

Now I have another question, I am more of a hardware than a software man so my menu code is probably a little overcomplicated more so than it should be, my whole program is controlled by IR commands so it is not as easy as setting a button to go in to a menu I will post the code below but was wondering if there is an easier way to do the menu, or an easier way to write to the EEPROM or DS1307 memory?

The code below is for the on/off time settings it basically flashes the time stored in weekOn, weekOff, weekendOn and weekendOff and if a button is pressed that increases the value, most of the code is used to set the buttons from the IR command and the LCD print out so where would I add the EEPROM command would there be an exit command in here to write it, the LED string is set in exactly the same way?

  ////////////
  // Menu 1 //
  ////////////
  if(menu == 1){
    if(forward == 1){
      if(next == 0){
        next = 1;
    } else {
      if(next == 1){
        next = 2;
      } else {
        if(next == 2){
          next = 0;
        }
      }
    }
  }
  
    if(back == 1){
      if(next == 2){
         next = 1;
         } else {
           if(next == 1){
              next = 0;
              } else {
                if(next == 0){
                   next = 2;
          }
        }
      }
    }
    
    if(next == 0){
    lcd.home();
    lcd.print("Set time on");
    Serial.println("Set time on");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.print(weekOn);
    lcd.print(":00");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.print(weekendOn);
    lcd.print(":00");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");
    }
    
    if(next == 1){ // Set Weekdays on time
      // Up/Down to change value
      if(up == 1){
        if(weekOn == 23){
          weekOn = 0;
        } else {
          weekOn ++;}
      }
      
      if(down == 1){
        if(weekOn == 0){
          weekOn = 23;
      } else {
        weekOn--;}
      } // End of value commands
      
    lcd.home();
    lcd.print("Set time on");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.print(weekendOn);
    lcd.print(":00");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");

      if(counter < flashCount){
        counter++;
        lcd.setCursor(9,1);
        lcd.print(weekOn);
        lcd.print(":00");
        flashCounter = flashCount;
      } else {
        if(flashCounter <= flashCount){
          flashCounter--;
          lcd.setCursor(9,1);
          lcd.print("     ");
        } if(flashCounter == 0){
          counter = 0;
        }
      }
    } else {
      lcd.setCursor(9,1);
      lcd.print(weekOn);
      lcd.print(":00");
    } // End of setting Weekdays on time
    
    if(next == 2){ // Set Tuesday on time
      // Up/Down to change value
      if(up == 1){
        if(weekendOn == 23){
          weekendOn = 0;
        } else {
          weekendOn ++;}
      }
      
      if(down == 1){
        if(weekendOn == 0){
          weekendOn = 23;
      } else {
        weekendOn--;}
      } // End of value commands
      
    lcd.home();
    lcd.print("Set time on");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.print(weekOn);
    lcd.print(":00");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");

      if(counter < flashCount){
        counter++;
        lcd.setCursor(9,2);
        lcd.print(weekendOn);
        lcd.print(":00");
        flashCounter = flashCount;
      } else {
        if(flashCounter <= flashCount){
          flashCounter--;
          lcd.setCursor(9,2);
          lcd.print("     ");
        } if(flashCounter == 0){
          counter = 0;
        }
      }
    } else {
      lcd.setCursor(9,2);
      lcd.print(weekendOn);
      lcd.print(":00");
    } // End of setting on time
  } // End of menu 1

  ////////////
  // Menu 2 //
  ////////////
  if(menu == 2){
        if(forward == 1){
      if(next == 0){
        next = 1;
    } else {
      if(next == 1){
        next = 2;
      } else {
        if(next == 2){
          next = 0;
        }
      }
    }
  }
  
    if(back == 1){
      if(next == 2){
         next = 1;
         } else {
           if(next == 1){
              next = 0;
              } else {
                if(next == 0){
                   next = 2;
          }
        }
      }
    }
    
    if(next == 0){
    lcd.home();
    lcd.print("Set time off");
    Serial.println("Set time off");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.print(weekOff);
    lcd.print(":00");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.print(weekendOff);
    lcd.print(":00");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");
    }
    
    if(next == 1){ // Set Weekdays on time
      // Up/Down to change value
      if(up == 1){
        if(weekOff == 23){
          weekOff = 0;
        } else {
          weekOff ++;}
      }
      
      if(down == 1){
        if(weekOff == 0){
          weekOff = 23;
      } else {
        weekOff--;}
      } // End of value commands
      
    lcd.home();
    lcd.print("Set time off");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.print(weekendOff);
    lcd.print(":00");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");

      if(counter < flashCount){
        counter++;
        lcd.setCursor(9,1);
        lcd.print(weekOff);
        lcd.print(":00");
        flashCounter = flashCount;
      } else {
        if(flashCounter <= flashCount){
          flashCounter--;
          lcd.setCursor(9,1);
          lcd.print("     ");
        } if(flashCounter == 0){
          counter = 0;
        }
      }
    } else {
      lcd.setCursor(9,1);
      lcd.print(weekOff);
      lcd.print(":00");
    } // End of setting Weekdays on time
    
    if(next == 2){ // Set Tuesday on time
      // Up/Down to change value
      if(up == 1){
        if(weekendOff == 23){
          weekendOff = 0;
        } else {
          weekendOff ++;}
      }
      
      if(down == 1){
        if(weekendOff == 0){
          weekendOff = 23;
      } else {
        weekendOff--;}
      } // End of value commands
      
    lcd.home();
    lcd.print("Set time off");
    lcd.setCursor(0,1);
    lcd.print("Weekdays ");
    lcd.print(weekOff);
    lcd.print(":00");
    lcd.setCursor(0,2);
    lcd.print("Weekends ");
    lcd.setCursor(0,3);
    lcd.print("J3=B J7=F F3=D F4=U");

      if(counter < flashCount){
        counter++;
        lcd.setCursor(9,2);
        lcd.print(weekendOff);
        lcd.print(":00");
        flashCounter = flashCount;
      } else {
        if(flashCounter <= flashCount){
          flashCounter--;
          lcd.setCursor(9,2);
          lcd.print("     ");
        } if(flashCounter == 0){
          counter = 0;
        }
      }
    } else {
      lcd.setCursor(9,2);
      lcd.print(weekendOff);
      lcd.print(":00");
    } // End of setting off time
  }

Thank you for pointing me in the right direction!

int weekOn = 15;
int weekOff = 22;

How many hours in your days? My days have 24 hours, which fits in a byte, which is far easier to write to/read from EEPROM.

PaulS:
How many hours in your days? My days have 24 hours, which fits in a byte, which is far easier to write to/read from EEPROM.

24 hours so the data will always be from 0 to 23 as for percentages that will be from 0 to 100 will they also fit in a byte (I think 1 byte = 255?)?
So should I change them from int weekOn to byte weekOn so it is easier to write to the memory?

Thanks,
Gavin

So should I change them from int weekOn to byte weekOn so it is easier to write to the memory?

Yes.

PaulS:

So should I change them from int weekOn to byte weekOn so it is easier to write to the memory?

Yes.

Thanks Paul,

So now I need to answer the question of just writing once I exit my menu if the value has changed so I need to compare the EEPROM/DS1307 and then make a change if the byte in weekOn has changed any ideas my Menu code is posted above?

So now I need to answer the question of just writing once I exit my menu if the value has changed so I need to compare the EEPROM/DS1307 and then make a change if the byte in weekOn has changed any ideas my Menu code is posted above?

I'd recommend the use of some punctuation in this sentence. It is very difficult to parse.

It seems that you have the right idea. Re-write the value in EEPROM only if has changed. Reading what is there, comparing it to what you want there, and writing only if the values are not the same involves only three lines of code, unless you count the optional curly braces.

Punctuation and Grammar were never my strong points!

Do you have an example of code I could use to write to the EEPROM if the value has changed?
If the EEPROM has no value stored in the first instance would it throw an error in to my program or would it have a 0 value against it (turn on/off at midnight?)

If the EEPROM has no value stored in the first instance would it throw an error in to my program or would it have a 0 value against it (turn on/off at midnight?)

If nothing has ever been written to the EEPROM address, you'll find that it contains 255, not 0. You'll need to read and see. Testing for this value shouldn't be a problem.

void updateEEPROM(byte newCrap, byte addr)
{
  byte oldCrap = EEPROM.read(addr);
  if(newCrap != oldCrap)
  {
     EEPROM.write(addr, newCrap);
  }
}

http://arduino.cc/en/Reference/EEPROM

Thank you PaulS!

I now have a working test and it will only write to the EEPROM after I exit my menu and only if the values have changed so that should give me a decent EEPROM life :grin: