New Project using EEMEM

I’m trying to make a program that prompts me to enter the following:

  1. last time I changed my pool filter
  2. Next time I want to change my pool filter

Then the screen should say “meter ready for use” then subtract the two values and tell me when my next pool filter is due. For some reason when I start the unit it goes right to saying “meter ready for use” without asking me to enter anything. Any ideas?

#include <avr/eeprom.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (5,6,9,10,11,12);

  //HARDWARE//  
int upButton = 8;
int downButton = 0;
int setButton = 1;
int Backlight = 4;
int led = 2;
  
  //eeprom addresses//
EEMEM unsigned long lastPoolFilter = 100;   
EEMEM unsigned long poolFilterSchedule = 101; 
EEMEM unsigned long nextPoolFilter = 102;   
EEMEM unsigned long nextPoolFilterVal = eeprom_read_dword(&poolFilterSchedule) - eeprom_read_dword(&lastPoolFilter);
float nextPoolFilterVal2;

  //BUTTON COUNTER & STATES //
int buttonCounter = 0;        // Counter for the number of button presses
int c;  
int upButtonState = 0;       // Current state of up button
int lastUpState;
int downButtonState = 0;     // Current state of down button
int lastDownState;
int setButtonState = 0;      // Current state of set button
int lastSetState = 0;     // Previous state of the button
int state = 0;
int lastState = -1;

unsigned long timer = 0;
unsigned long start = millis();
  
void setup()
{
  lcd.begin(16,2);
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(setButton, INPUT_PULLUP);
  pinMode(led,OUTPUT);
  pinMode(Backlight,OUTPUT);

  digitalWrite(Backlight,HIGH);
  digitalWrite(led,LOW);
}


void loop()
{
  byte value  = eeprom_read_dword(&poolFilterSchedule);
  
  while ((value==0 ) || (value==0xff ))
  {
    line1();
    lcd.print("Last Pool Filter:");
    hrAgo();
    
  if (buttonCounter <=0)
  {
    line2b();
    print0();
  }
  
    debounce();
    
  if (setButtonState == LOW)
  { 
    c=9;
    eeprom_write_dword(&lastPoolFilter,buttonCounter);
    hrSet();
  }
    
  while (c==9)
  {
    schEvery();     
      
  if (setButtonState==LOW)
  { 
    c=87;
    eeprom_write_dword(&poolFilterSchedule, buttonCounter);
    nextPoolFilterVal = eeprom_read_dword(&poolFilterSchedule) - eeprom_read_dword(&lastPoolFilter);
    eeprom_write_dword(&nextPoolFilter, nextPoolFilterVal);
    schSet();
  }
  }     
      Clear();
      lcd.setCursor(1,0);
      lcd.print("Meter Is Ready");
      lcd.setCursor(3,1);
      lcd.print("For Use! ");
      delay(750);
      break;
    
  }
                  
                      
  while (value >=0 && value <255)
  {
    int upButtonState = digitalRead(upButton);
    int setButtonState = digitalRead(setButton);                        
  
  if (upButtonState == HIGH && lastUpState == LOW)
  {   
    delay(125);
    state++;
  
  if (state > 0) state = 0;                           
    
  }
                      
    lastUpState = upButtonState;                        
    int downButtonState = digitalRead(downButton);
  
  if (downButtonState == HIGH && lastDownState == LOW)
  { 
    delay(125);                         
    state--;
  
  if (state < 0) state = 0;
    
  }
    
    lastDownState = downButtonState;                        
  
  if (lastState != state)
  {                                           
      switch (state)
      {
        case 0:
        Replay1();
        break;
        case 1:                                                     
        Replay1();                                                  
        break;                            
        default:
        ;                         
    }
  
  lastState = state;
  }
  }
  }

  
////////////////////FUNCTIONS//////////////////////////////////////////////////////////////////////

////////////////////BUTTON DEBOUNCING AND ADDING//////////////////////////////////////////////////////////////
 void debounce(){
    upButtonState = digitalRead(upButton);      // read the pushbutton up input pin:
    if (upButtonState != lastSetState)       // compare the buttonState to its previous state
       {delay(40);
      if (upButtonState == LOW)        // if the state has changed, increment the counter     
         {buttonCounter++;
          line2();     
        if (buttonCounter<100) lcd.print (" ");        //Keeps digits in one place and from creating extra zeros
          if (buttonCounter<10) lcd.print (" ");         //Keeps digits in one place and from creating extra zeros
            if (buttonCounter>=1) lcd.print (buttonCounter);}}         
               lastSetState = upButtonState;       // save the current state as the last state, for next time through the loop
               downButtonState = digitalRead(downButton);      // read the pushbutton down input pin:
              if (downButtonState != lastSetState) {        // compare the buttonState to its previous state
                 delay(40);
                 if (downButtonState == LOW){      // if the state has changed, decrement the counter   
                   if (buttonCounter>=1) {
                       buttonCounter--;}
                        line2();
                     if (buttonCounter<100) lcd.print (" ");  //Keeps digits in one place and from creating extra zeros. try removing this code later
                       if (buttonCounter<10) lcd.print (" ");  //Keeps digits in one place and from creating extra zeros
                         if (buttonCounter<1) print0();  //Keeps digits in one place and from creating extra zeros
                           if (buttonCounter>=1) lcd.print (buttonCounter);}}
                               lastSetState = downButtonState; // save the current state as the last state, for next time through the loop  
  
   setButtonState = digitalRead(setButton);}  
   ///////////////////////////////////////////////////////////////////////////////////////////////
         void Replay1(){
  Clear();   
    lcd.setCursor(0,0);
    lcd.print("Air Filter In:");
  nextPoolFilterVal = eeprom_read_dword(&poolFilterSchedule) - eeprom_read_dword(&lastPoolFilter);
  nextPoolFilterVal2 = (nextPoolFilterVal);
  lcd.setCursor(1,1);
    lcd.print(nextPoolFilterVal2,1);
    lcd.print(" Hours");}
   /////////////////////////////////////////////////////////////////////////////////////////////  
        void schEvery(){
    lcd.setCursor(0,0);   
    lcd.print("Schedule Every:");
    lcd.setCursor (4,1);
    lcd.print("Hours");
    if (buttonCounter <=0){
    lcd.setCursor(2,1);
    lcd.print ("0");}             
    debounce();}
    ///////////////////////////////////////////////////////////////////////////
         void schSet(){
    Clear();
    lcd.setCursor(1,0);
    lcd.print("Schedule Set!");    
    delay(750);    
    buttonCounter=0;}
    ///////////////////////////////////////////////////////////////////////////
        void hrAgo(){
    lcd.setCursor(4,1);
    lcd.print("Hours Ago");}
    ///////////////////////////////////////////////////////////////////////////
        void hrSet(){
    Clear();
    lcd.setCursor(2,0);
    lcd.print("Hours Set!");
    delay(750);
    Clear();
    buttonCounter=0;}
  ///////////////////////////////////////////////////////////////////////////
    void print0(){
    lcd.print("0");}
    ///////////////////////////////////////////////////////////////////////////
        void Clear(){
        lcd.clear();}
    ///////////////////////////////////////////////////////////////////////////
    void line1(){
    lcd.setCursor(0,0);}
  ///////////////////////////////////////////////////////////////////////////      
    void line2(){
    lcd.setCursor(0,1);}
  ///////////////////////////////////////////////////////////////////////////
    void line2b(){
    lcd.setCursor(2,1);}
EEMEM unsigned long lastPoolFilter = 100;   
EEMEM unsigned long poolFilterSchedule = 101;
EEMEM unsigned long nextPoolFilter = 102;   
EEMEM unsigned long nextPoolFilterVal = eeprom_read_dword(&poolFilterSchedule) - eeprom_read_dword(&lastPoolFilter);

Have you uploaded / written those values to EEPROM?

You can use EEMEM, like PROGMEM to place a variable in the EEPROM memory space, however the IDE has disabled overwriting the EEPROM and anything you assign to these variables is not set.

You can implement a check for some known data to see whether its a first run, and set these defaults.