Using EEPROM

Hi,

I have been working on some code recently for a automatic dosing pump of an aquarium. Because i need the arduino to remember time and dose rates even if the power goes out ive been writing these to EEPROM.
My code worked well for the first set of menu items. However i can not get it to work for the second set. Which are exactly the same however have different variable names.
What is being returned is the number 255 initially and it seems like the arduino is trying to re write all the time because the lcd is faintly flashes at a high speed.

//Menu 7 - MICRO TIME SET
if (MenuCounter == 7) {
//print heading
lcd.setCursor(0,0);
lcd.print(“MICRO TIME SET”);
lcd.setCursor(0,1);
MICROHR = EEPROM.read(12);
lcd.print(MICROHR);
lcd.setCursor(2,1);
lcd.print(":");
lcd.setCursor(3,1);
MICROMIN = EEPROM.read(13);
lcd.print(MICROMIN);
lcd.setCursor(5,1);
lcd.print(":00");

if (ButtonSelect.uniquePress()) {
SelectCounter++;
}

if (SelectCounter == 0) {
if (ButtonUp.uniquePress()){
if (MICROHR<23) {
MICROHR++;
EEPROM.write(12,MICROHR);
lcd.clear();
}
}
if (ButtonDown.uniquePress()) {
if (MICROHR>0) {
MICROHR–;
EEPROM.write(12,MICROHR);
lcd.clear();
}
}
}
if (SelectCounter == 1) {
if (ButtonUp.uniquePress()){
if (MICROMIN<59) {
MICROMIN++;
EEPROM.write(13,MICROMIN);
lcd.clear();
}
}
if (ButtonDown.uniquePress()) {
if (MICROMIN>0) {
MICROMIN–;
EEPROM.write(13,MICROMIN);
lcd.clear();
}
}
}
if (SelectCounter == 2) {
SelectCounter = 0;
}
}

If anybody could offer any assistance that would be great.
Cheers, Matt

The fault is usually in the code you DON'T post. When posting please surround your code with [code] and [/code] tags.

From your description I can't even work out WHICH part is not working.

You need also to add more code. Why don’t you post the entire code?

Ok ive added the entire code, and used the
code tags. Hopefully this makes some more sense.
Where i am having problems is within menu 7 and 8. The other menus are currently working well and the use of EEPROM is going as planned, except in 7 and 8.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Button.h>
#include <EEPROM.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(7,8,9,10,11,12);

//Declare variables 

Button ButtonUp = Button(4,BUTTON_PULLDOWN);
Button ButtonDown = Button(5,BUTTON_PULLDOWN);
Button ButtonMenu = Button(2,BUTTON_PULLDOWN);
Button ButtonSelect = Button(6,BUTTON_PULLDOWN);

int MenuCounter = 0;
int SelectCounter = 0;
int MACROML = 0;
int MICROML = 0;
int MACROHR = 0;
int MACROMIN = 0;
int MICROHR = 0;
int MICROMIN = 0; 
int MICRODAYA = 7; //7 is not a day
int MICRODAYB = 7; //7 is not a day
int MICRODAYC = 7; //7 is not a day
int MICRODAYD = 7; //7 is not a day
int MICRODAYE = 7; //7 is not a day
int MICRODAYF = 7; //7 is not a day
int MICRODAYG = 7; //7 is not a day
int MACRODAYA = 7; //7 is not a day
int MACRODAYB = 7; //7 is not a day
int MACRODAYC = 7; //7 is not a day
int MACRODAYD = 7; //7 is not a day
int MACRODAYE = 7; //7 is not a day
int MACRODAYF = 7; //7 is not a day
int MACRODAYG = 7; //7 is not a day


//Set up arduino - only runs on bootup
void setup () {
  Serial.begin(57600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
     rtc.adjust(DateTime(2014, 11, 20, 1, 41, 0));
  }
//LCD start
lcd.begin(16,2);

//Declare pin modes
pinMode(3,OUTPUT);
pinMode(13,OUTPUT);


} //Close Void Setup

//Begin looping - main code
void loop () {
    DateTime now = rtc.now();
    
//Menu screen counter    
    if (ButtonMenu.uniquePress()) {
      if (MenuCounter<8){    
      MenuCounter++;
      lcd.clear();
      } 
    else {
    MenuCounter = 0; 
    lcd.clear();
          }
    }
    
//Menu 0 - Current Time
    if (MenuCounter == 0) {
   
   
    //Print Text on line 0
    //lcd.clear();
    lcd.setCursor(2,0);    
    lcd.print("Current Time");
    
    lcd.setCursor(4,1);
    //Print the time
    lcd.print(now.hour());
    lcd.print(":");
    lcd.print(now.minute());
    lcd.print(":");
    lcd.print(now.second());
    }
       
//Menu 1 - MACRO'S
    if (MenuCounter == 1) {
      
      //Print heading and display dose rate
      //lcd.clear();
      MACROML = EEPROM.read(0);
      lcd.setCursor(0,0);
      lcd.print("MACRO DOSE");
      lcd.setCursor(0,1);
      lcd.print(MACROML);
      lcd.setCursor(3,1);
      lcd.print("mL");
      
      if (ButtonUp.uniquePress()) {
        if (MACROML<100) {
          MACROML++;
          EEPROM.write(0,MACROML);
           lcd.clear();
          }
      }
      if (ButtonDown.uniquePress()) {
        if (MACROML>0) {
        MACROML--;
        EEPROM.write(0,MACROML);
        lcd.clear();
        } 
      }
      
      } // Close Menu 1 if statement
    
    
//Menu 2 - MACRO OVERRIDE
    if (MenuCounter == 2) {
      
      //Print heading and display dose rate
      //lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("MACRO OVERRIDE");
      lcd.setCursor(0,1);
      lcd.print("Press Select");
            
      if (ButtonSelect.isPressed()) {
        digitalWrite(13,HIGH);
      }
      else {
        digitalWrite(13,LOW);
           }
    } // Close Menu 2 if statement    
    
    
//Menu 3 - MACRO TIME SET
  if (MenuCounter == 3) {
    //print heading
    lcd.setCursor(0,0);
    lcd.print("MACRO TIME SET");
    lcd.setCursor(0,1);
    MACROHR = EEPROM.read(3);
    lcd.print(MACROHR);
    lcd.setCursor(2,1);
    lcd.print(":");
    lcd.setCursor(3,1);
    MACROMIN = EEPROM.read(4);
    lcd.print(MACROMIN);
    lcd.setCursor(5,1);
    lcd.print(":00");
    
    if (ButtonSelect.uniquePress()) {
      SelectCounter++;
    }
    
    if (SelectCounter == 0) {          
      if (ButtonUp.uniquePress()){
        if (MACROHR<23) {
        MACROHR++;
        EEPROM.write(3,MACROHR);
        lcd.clear();
        }
      }
        if (ButtonDown.uniquePress()) {
          if (MACROHR>0) {
          MACROHR--;
          EEPROM.write(3,MACROHR);
          lcd.clear();
          }
        }
     }  
     if (SelectCounter == 1) {
      if (ButtonUp.uniquePress()){
        if (MACROMIN<59) {
        MACROMIN++;
        EEPROM.write(4,MACROMIN);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACROMIN>0) {
        MACROMIN--;
        EEPROM.write(4,MACROMIN);
        lcd.clear();
        }
      }  
    }
      if (SelectCounter == 2) {
      SelectCounter = 0;
      }  
  }
  
//Menu 4 - MACRO DAYSET
  if (MenuCounter == 4) {
    //print heading
    lcd.setCursor(0,0);
    lcd.print("MACRO DAY SET");
    lcd.setCursor(0,1);
    MACRODAYA = EEPROM.read(5);
    lcd.print(MACRODAYA);
    lcd.setCursor(2,1);
    MACRODAYB = EEPROM.read(6);
    lcd.print(MACRODAYB);
    lcd.setCursor(4,1);
    MACRODAYC = EEPROM.read(7);
    lcd.print(MACRODAYC);
    lcd.setCursor(6,1);
    MACRODAYD = EEPROM.read(8);
    lcd.print(MACRODAYD);
    lcd.setCursor(8,1);
    MACRODAYE = EEPROM.read(9);
    lcd.print(MACRODAYE);
    lcd.setCursor(10,1);
    MACRODAYF = EEPROM.read(10);
    lcd.print(MACRODAYF);
    lcd.setCursor(12,1);
    MACRODAYG = EEPROM.read(11);
    lcd.print(MACRODAYG);
    
    if (ButtonSelect.uniquePress()) {
      SelectCounter++;
    }
    
    if (SelectCounter == 0) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYA<7) {
        MACRODAYA++;
        EEPROM.write(5,MACRODAYA);
        lcd.clear();
        }
      }
       if (ButtonDown.uniquePress()) {
        if (MACRODAYA>0) {
        MACRODAYA--;
        EEPROM.write(5,MACRODAYA);
        lcd.clear();
        }
      }
    }  
     if (SelectCounter == 1) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYB<7) {
        MACRODAYB++;
        EEPROM.write(6,MACRODAYB);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYB>0) {
        MACRODAYB--;
        EEPROM.write(6,MACRODAYB);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 2) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYC<7) {
        MACRODAYC++;
        EEPROM.write(7,MACRODAYC);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYC>0) {
        MACRODAYC--;
        EEPROM.write(7,MACRODAYC);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 3) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYD<7) {
        MACRODAYD++;
        EEPROM.write(8,MACRODAYD);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYD>0) {
        MACRODAYD--;
        EEPROM.write(8,MACRODAYD);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 4) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYE<7) {
        MACRODAYE++;
        EEPROM.write(9,MACRODAYE);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYE>0) {
        MACRODAYE--;
        EEPROM.write(9,MACRODAYE);
        lcd.clear();
        }
      }  
    }
     if (SelectCounter == 5) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYF<7) {
        MACRODAYF++;
        EEPROM.write(10,MACRODAYF);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYF>0) {
        MACRODAYF--;
        EEPROM.write(10,MACRODAYF);
        lcd.clear();
        }
      }  
    }
     if (SelectCounter == 6) {
      if (ButtonUp.uniquePress()){
        if (MACRODAYG<7) {
        MACRODAYG++;
        EEPROM.write(11,MACRODAYG);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MACRODAYG>0) {
        MACRODAYG--;
        EEPROM.write(11,MACRODAYG);
        lcd.clear();
        }
      }  
    }
      if (SelectCounter == 7) {
      SelectCounter = 0;
      }  
}

[/code]

i have split the code into two in order to fit it in the post. this is the second half which is where the errors are occurring.

//Menu 5 - MICRO'S
    if (MenuCounter == 5) {
      
      //Print heading and display dose rate
      //lcd.clear();
      MICROML = EEPROM.read(1);
      lcd.setCursor(0,0);
      lcd.print("MICRO DOSE");
      lcd.setCursor(0,1);
      lcd.print(MICROML);
      lcd.setCursor(3,1);
      lcd.print("mL");
      
      if (ButtonUp.uniquePress()) {
        if (MICROML<100){
        MICROML++;
        EEPROM.write(1,MICROML);
        lcd.clear();
        }
      }
      
      if (ButtonDown.uniquePress()) {
        if (MICROML>0){ 
        MICROML--;
        EEPROM.write(1,MICROML);
        lcd.clear();
        }
      } 
    } // Close Menu 5 If statement

//Menu 6 - MICRO OVERRIDE
    if (MenuCounter == 6) {
      
      //Print heading and display dose rate
      //lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("MICRO OVERRIDE");
      lcd.setCursor(0,1);
      lcd.print("Press Select");
            
      if (ButtonSelect.isPressed()) {
        digitalWrite(13,HIGH);
      }
      else {
        digitalWrite(13,LOW);
           }
    } // Close Menu 6 if statement

//Menu 7 - MICRO TIME SET
  if (MenuCounter == 7) {
    //print heading
    lcd.setCursor(0,0);
    lcd.print("MICRO TIME SET");
    lcd.setCursor(0,1);
    MICROHR = EEPROM.read(12);
    lcd.print(MICROHR);
    lcd.setCursor(2,1);
    lcd.print(":");
    lcd.setCursor(3,1);
    MICROMIN = EEPROM.read(13);
    lcd.print(MICROMIN);
    lcd.setCursor(5,1);
    lcd.print(":00");
   
    if (ButtonSelect.uniquePress()) {
      SelectCounter++;
    }
    
    if (SelectCounter == 0) {          
      if (ButtonUp.uniquePress()){
        if (MICROHR<23) {
        MICROHR++;
        EEPROM.write(12,MICROHR);
        lcd.clear();
        }
      }
        if (ButtonDown.uniquePress()) {
          if (MICROHR>0) {
          MICROHR--;
          EEPROM.write(12,MICROHR);
          lcd.clear();
          }
        }
     }  
     if (SelectCounter == 1) {
      if (ButtonUp.uniquePress()){
        if (MICROMIN<59) {
        MICROMIN++;
        EEPROM.write(13,MICROMIN);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICROMIN>0) {
        MICROMIN--;
        EEPROM.write(13,MICROMIN);
        lcd.clear();
        }
      }  
    }
      if (SelectCounter == 2) {
      SelectCounter = 0;
      }  
  }
  
  
  
//Menu 8 - MICRO DYAYSET
  if (MenuCounter == 8) {
    //print heading
    lcd.setCursor(0,0);
    lcd.print("MICRO DAY SET");
    lcd.setCursor(0,1);
    MICRODAYA = EEPROM.read(14);
    lcd.print(MICRODAYA);
    lcd.setCursor(2,1);
    MICRODAYB = EEPROM.read(15);
    lcd.print(MICRODAYB);
    lcd.setCursor(4,1);
    MICRODAYC = EEPROM.read(16);
    lcd.print(MICRODAYC);
    lcd.setCursor(6,1);
    MICRODAYD = EEPROM.read(17);
    lcd.print(MICRODAYD);
    lcd.setCursor(8,1);
    MICRODAYE = EEPROM.read(18);
    lcd.print(MICRODAYE);
    lcd.setCursor(10,1);
    MICRODAYA = EEPROM.read(19);
    lcd.print(MICRODAYF);
    lcd.setCursor(12,1);
    MICRODAYG = EEPROM.read(20);
    lcd.print(MICRODAYG);
    
    if (ButtonSelect.uniquePress()) {
      SelectCounter++;
    }
    
    if (SelectCounter == 0) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYA<7) {
        MICRODAYA++;
        EEPROM.write(14,MICRODAYA);
        lcd.clear();
        }
      }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYA>0) {
        MICRODAYA--;
        EEPROM.write(14,MICRODAYA);
        lcd.clear();
        }
      }
    }  
     if (SelectCounter == 1) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYB<7) {
        MICRODAYB++;
        EEPROM.write(15,MICRODAYB);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYB>0) {
        MICRODAYB--;
        EEPROM.write(15,MICRODAYB);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 2) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYC<7) {
        MICRODAYC++;
        EEPROM.write(16,MICRODAYC);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYC>0) {
        MICRODAYC--;
        EEPROM.write(16,MICRODAYC);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 3) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYD<7) {
        MICRODAYD++;
        EEPROM.write(17,MICRODAYD);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYD>0) {
        MICRODAYD--;
        EEPROM.write(17,MICRODAYD);
        lcd.clear();
        }
      }  
    }
    if (SelectCounter == 4) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYE<7) {
        MICRODAYE++;
        EEPROM.write(18,MICRODAYE);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYE>0) {
        MICRODAYE--;
        EEPROM.write(18,MICRODAYE);
        lcd.clear();
        }
      }  
    }
     if (SelectCounter == 5) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYF<7) {
        MICRODAYF++;
        EEPROM.write(19,MICRODAYF);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYF>0) {
        MICRODAYF--;
        EEPROM.write(19,MICRODAYF);
        lcd.clear();
        }
      }  
    }
     if (SelectCounter == 6) {
      if (ButtonUp.uniquePress()){
        if (MICRODAYG<7) {
        MICRODAYG++;
        EEPROM.write(20,MICRODAYG);
        lcd.clear();
        }
     }
        if (ButtonDown.uniquePress()) {
        if (MICRODAYG>0) {
        MICRODAYG--;
        EEPROM.write(20,MICRODAYG);
        lcd.clear();
        }
      }  
    }
      if (SelectCounter == 7) {
      SelectCounter = 0;
      }  
}  
  
//Backlight ON
 digitalWrite(3,255);  


} //Close loop

What do you call the "first set of menu items"?

In my original post i was referring to all menus other then 7 and 8 as the first set. What is happening is, i am using a counter called MenuCounter to count pushes of the 'menu' button and then display the appropriate 'menu' on the lcd screen with the related values. The menus are operating well, my problem is that in menu 7 and menu 8 (i.e. the MICRO time and day menus) the EEPROM data is not working correctly. initially all values display as 255, then if you adjust these values using the ButtonUp or ButtonDown then the new values are being written to EEPROM and will be remembered. However it screen is flashing faintly in these menus and the push buttons are only working sometimes. It seems to me like as though the EEPROM is continually writing or the lcd is resetting or something.

int MACROML = 0;
int MICROML = 0;
int MACROHR = 0;
int MACROMIN = 0;
int MICROHR = 0;
int MICROMIN = 0; 
int MICRODAYA = 7; //7 is not a day

By convention, all capital letter names are constants (like INPUT_PULLUP). Where is your const keyword?

I dont intend for these to be constants, they need to be able to be changed within the program. Apart from not being convention does it matter if these are in capitals?

mattricho: I dont intend for these to be constants, they need to be able to be changed within the program. Apart from not being convention does it matter if these are in capitals?

It could if you want others to help debug your code. To be honest, it threw me. I took one glimpse at the code and assumed it was referring to values that had been defined in a library that I'm unfamiliar with. So didn't look any further.

OK Had another look through the code. Looking at line 412 onward, I take it this is the part where you are getting 255 returned for your eeprom reads?

I notice you are reading from addresses 12 and 13.

So doing a swoop through looking for EEPROM.write statements I find that there are just two lines that potentially write to address 12.

These are on lines 435 and 442. Might I suggest that you put a Serial.write just after these lines to check that your code is actually executing these lines and writing something you expect.

I have found that if i push the down button many many times the value for example for MICROHR will decrease and then jump to 99, from here it will decrease untill it is within the limits i have placed which are 59 and 0. once the value is within these limits it behaves as expected. Is there a process required to initilize eeprom values?

mattricho:
I have found that if i push the down button many many times the value for example for MICROHR will decrease and then jump to 99, from here it will decrease untill it is within the limits i have placed which are 59 and 0. once the value is within these limits it behaves as expected. Is there a process required to initilize eeprom values?

No. All EEPROM addresses hold 255 until they are written to.