EEPROM values corrupted

Hello

I have been playing around with EEPROM and try to set it up for a water dispensing device.
When I start the Arduino Mega, and the EEPROM was cleared beforehand, I read all 3 variables to be 0. So that seems to work. When I then start assigning values to the variables the serial monitor gives me the right values, however when switching it off and back on a again the values are not correct. Depending how defect they are e.g. if something like water_amount = 2500 and water_temperature = 1500, if I try to change the values for one of them, the other one is affected as well.

I can't seem to find my mistake...

#include "Button.h"
#include <LiquidCrystal.h> // include the LiquidCrystal library
#include <LiquidMenu.h>
#include <EEPROM.h>

// initialize the library with the LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// define the button pins
  const bool pullup = true;
  Button Down_Button(6, pullup);
  Button Up_Button(7, pullup);
  Button Select_Button(8, pullup);
  Button Back_Button(9,pullup);
  Button Spare_Button(10,pullup);

/* An enumerator can be used for the callback functions. It sets the number
 * for similar functions. Later when a function is attached it can be passed
 * with this enum rather than a magic number.
 */
  enum FunctionTypes {
    increase = 1,
    decrease = 2,
  };

//Variables Definition
    // Water Variables Definition
        //Water Amount
            int water_amount;
            int max_water_amount = 250;     //set the max water amount
            int min_water_amount = 0;       //set the min water amount
            int increment_water_amount = 5; //set the increments that the user can change on the display
            int ee_address_water_amount = 0;

        //Water Temperature
            int water_temperature;
            int max_water_temperature = 90;       //set max water temperature, needs to be evaluated through testing
            int min_water_temperature = 0;        //set min water temperature, this should probably be the temperature of the water in the tank
            int increment_water_temperature = 5;  //set the increments that the user can change on the display
            int ee_address_water_temperature = 1;

        //Water Flow Rate    
            int water_flow_rate;
            int max_water_flow_rate = 100;        //set max water flow rate, needs to be evaluated through testing
            int min_water_flow_rate = 0;          //set min water flow rate, what is actually feasible, do we actually allow the user to change this
            int increment_water_flow_rate = 5;    //how accurate can the machine dispense the water
            int ee_address_water_flow_rate = 2; 

//Menu Setup

      //Main Menu
        LiquidLine main_line_1(0, 0, "Water   Set.");
          LiquidScreen smain_1(main_line_1);
            LiquidMenu mmain(lcd, smain_1, 1);

      //Water Menu
        LiquidLine water_line_1(0, 0, "W_Amount", water_amount);
        LiquidLine water_line_2(0, 0, "W_Temp.", water_temperature);
        LiquidLine water_line_3(0, 0, "Flow Speed", water_flow_rate);
        LiquidLine mmback_line(0, 0, "Back");
          LiquidScreen swater_1(water_line_1, water_line_2, water_line_3, mmback_line);
            LiquidMenu mwater(lcd, swater_1);

      //Overall Menu
              LiquidSystem overall_menu(mmain, mwater);

  void buttonsCheck() {
    if (Down_Button.check() == LOW) {
      overall_menu.next_screen();
          Serial.print("Down_Button Pushed");
          Serial.println();
    }
    if (Up_Button.check() == LOW) {
      overall_menu.previous_screen();
          Serial.print("Up_Button Pushed");
          Serial.println();
    }
    if (Select_Button.check() == LOW) {
      overall_menu.call_function(increase);
          Serial.print("Select_Button Pushed");
          Serial.println();
    }
    if (Back_Button.check() == LOW) {
      overall_menu.call_function(decrease);
          Serial.print("Back_Button Pushed");
          Serial.println();
    }
    if (Spare_Button.check() == LOW) {
      overall_menu.switch_focus();
          Serial.print("Spare_Button Pushed");
          Serial.println();
    }
  }
//Value Change Functions
    //Water Functions
        //Water Amount
            //Increase Water Amount
                void increase_water_amount() {
                  if (water_amount < max_water_amount) {
                    water_amount += increment_water_amount;
                        Serial.print("Increase Water_Amount called");
                        Serial.println();
                        //Save Input
                                EEPROM.put(ee_address_water_amount, water_amount);
                                      Serial.print("Water Amount saved ");
                                      Serial.print(water_amount);
                                      Serial.println();
                  } else {
                    water_amount = max_water_amount;
                        Serial.print("Max Water Amount reached");
                        Serial.println();
                  }
                }

            //Decrease Water Amount
                void decrease_water_amount() {
                  if (water_amount > min_water_amount) {
                    water_amount -= increment_water_amount;
                        Serial.print("Decrease Water Amount called");
                        Serial.println();
                            //Save Input
                                EEPROM.put(ee_address_water_amount, water_amount);
                                      Serial.print("Water Amount saved ");
                                      Serial.print(water_amount);
                                      Serial.println();
                  } else {
                    water_amount = min_water_amount;
                        Serial.print("Min Water Amount reached");
                        Serial.println();
                  }
                }

        //Water Temperature
            //Increase Water Temperature
                void increase_water_temperature() {
                  if (water_temperature < max_water_temperature) {
                    water_temperature += increment_water_temperature;
                        Serial.print("Increase Water_Temperature called");
                        Serial.println();
                        //Save Input
                                EEPROM.put(ee_address_water_temperature, water_temperature);
                                      Serial.print("Water Temperature saved ");
                                      Serial.print(water_temperature);
                                      Serial.println();
                  } else {
                    water_temperature = max_water_temperature;
                        Serial.print("Max Water Temperature reached");
                        Serial.println();
                  }
                }

            //Decrease Water Temperature
                void decrease_water_temperature() {
                  if (water_temperature > min_water_temperature) {
                    water_temperature -= increment_water_temperature;
                        Serial.print("Decrease Water Temperature called");
                        Serial.println();
                            //Save Input
                                EEPROM.put(ee_address_water_temperature, water_temperature);
                                      Serial.print("Water Temperature saved ");
                                      Serial.print(water_temperature);
                                      Serial.println();
                  } else {
                    water_temperature = min_water_temperature;
                        Serial.print("Min Water Temperature reached");
                        Serial.println();
                  }
                }

        //Water Flow
            //Increase Water Flow
                void increase_water_flow_rate() {
                  if (water_flow_rate < max_water_flow_rate) { //this has to be evaluated, what is a feasible flow rate
                    water_flow_rate += increment_water_flow_rate;     //what is a feasible increase / decrease value
                        Serial.print("Increase Water_Flow_rate called");
                        Serial.println();
                        //Save Input
                                EEPROM.put(ee_address_water_flow_rate, water_flow_rate);
                                      Serial.print("Water Flow_Rate saved ");
                                      Serial.print(water_flow_rate);
                                      Serial.println();
                  } else {
                    water_flow_rate = max_water_flow_rate;
                        Serial.print("Max Water Flow Rate reached");
                        Serial.println();
                  }
                }

            //Decrease Water Flow
                void decrease_water_flow_rate() {
                  if (water_flow_rate > min_water_flow_rate) {
                    water_flow_rate -= increment_water_flow_rate;
                        Serial.print("Decrease Water Flow Rate called");
                        Serial.println();
                            //Save Input
                                    EEPROM.put(ee_address_water_flow_rate, water_flow_rate);
                                          Serial.print("Water Flow_Rate saved ");
                                          Serial.print(water_flow_rate);
                                          Serial.println();
                  } else {
                    water_flow_rate = min_water_flow_rate;
                        Serial.print("Min Water Temperature reached");
                        Serial.println();
                  }
                }

    // Used for attaching something to the lines, to make them focusable.
        void fBlankFunction() {
            return;
        }

    //Function to switch to the Water Menu
        void goto_mwater() {
            overall_menu.change_menu(mwater);
                Serial.print("Go To Water Menu");
                Serial.println();
        }
    //Callback function that will be attached to back_line to Main Menu
        void mm_go_back(){
          overall_menu.change_menu(mmain);
              Serial.print("Go To Main Menu");
              Serial.println();
          }

void setup() {

  //Start Serial Monitor and print the same text as on the LCD
      Serial.begin(9600);

  //Read the values recorded in the EEPROM
      //Water Values
          //Water Amount
              EEPROM.get(ee_address_water_amount, water_amount);
                  Serial.print("The Water Amount stored on the EEPDROM is ");
                  Serial.print(water_amount);
                  Serial.println();
          //Water Temperature
              EEPROM.get(ee_address_water_temperature, water_temperature);
                  Serial.print("The Water Temperature stored on the EEPDROM is ");
                  Serial.print(water_temperature);
                  Serial.println();
          //Water Flow Rate
              EEPROM.get(ee_address_water_flow_rate, water_flow_rate);
                  Serial.print("The Water Flow Rate stored on the EEPDROM is ");
                  Serial.print(water_flow_rate);
                  Serial.println();

  //Start LCD Screen
        lcd.begin(16, 2);

  //Attaching a function to the lines is required for scrolling to work
    //Main
      main_line_1.attach_function(1, goto_mwater);
      mmback_line.attach_function(1, mm_go_back);

    //Water
      water_line_1.attach_function(increase, increase_water_amount);
      water_line_1.attach_function(decrease, decrease_water_amount);
      water_line_2.attach_function(increase, increase_water_temperature);
      water_line_2.attach_function(decrease, decrease_water_temperature);
      water_line_3.attach_function(increase, increase_water_flow_rate);
      water_line_3.attach_function(decrease, decrease_water_flow_rate);

    // Set the number of lines the display has
      //Main
        smain_1.set_displayLineCount(2);

      //Water
        swater_1.set_displayLineCount(2);

    mmain.add_screen(smain_1);
    mwater.add_screen(swater_1);

  overall_menu.update();
}

void loop() {
	buttonsCheck();



}

From your description, it sounds like you have got the code uploading to the Arduino and it's not behaving as expected. But the code you posted cannot be uploaded because it contains errors.

1 Like

Sorry I made a mistake when copying over. I updated the code above now.

What is the serial output, if you press all the buttons in sequence?

Here is the sketch, auto-formatted in a standard way:

#include "Button.h"
#include <LiquidCrystal.h> // include the LiquidCrystal library
#include <LiquidMenu.h>
#include <EEPROM.h>

// initialize the library with the LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// define the button pins
const bool pullup = true;
Button Down_Button(6, pullup);
Button Up_Button(7, pullup);
Button Select_Button(8, pullup);
Button Back_Button(9, pullup);
Button Spare_Button(10, pullup);

/* An enumerator can be used for the callback functions. It sets the number
   for similar functions. Later when a function is attached it can be passed
   with this enum rather than a magic number.
*/
enum FunctionTypes {
  increase = 1,
  decrease = 2,
};

//Variables Definition
// Water Variables Definition
//Water Amount
int water_amount;
int max_water_amount = 250;     //set the max water amount
int min_water_amount = 0;       //set the min water amount
int increment_water_amount = 5; //set the increments that the user can change on the display
int ee_address_water_amount = 0;

//Water Temperature
int water_temperature;
int max_water_temperature = 90;       //set max water temperature, needs to be evaluated through testing
int min_water_temperature = 0;        //set min water temperature, this should probably be the temperature of the water in the tank
int increment_water_temperature = 5;  //set the increments that the user can change on the display
int ee_address_water_temperature = 1;

//Water Flow Rate
int water_flow_rate;
int max_water_flow_rate = 100;        //set max water flow rate, needs to be evaluated through testing
int min_water_flow_rate = 0;          //set min water flow rate, what is actually feasible, do we actually allow the user to change this
int increment_water_flow_rate = 5;    //how accurate can the machine dispense the water
int ee_address_water_flow_rate = 2;

//Menu Setup

//Main Menu
LiquidLine main_line_1(0, 0, "Water   Set.");
LiquidScreen smain_1(main_line_1);
LiquidMenu mmain(lcd, smain_1, 1);

//Water Menu
LiquidLine water_line_1(0, 0, "W_Amount", water_amount);
LiquidLine water_line_2(0, 0, "W_Temp.", water_temperature);
LiquidLine water_line_3(0, 0, "Flow Speed", water_flow_rate);
LiquidLine mmback_line(0, 0, "Back");
LiquidScreen swater_1(water_line_1, water_line_2, water_line_3, mmback_line);
LiquidMenu mwater(lcd, swater_1);

//Overall Menu
LiquidSystem overall_menu(mmain, mwater);

void buttonsCheck() {
  if (Down_Button.check() == LOW) {
    overall_menu.next_screen();
    Serial.print("Down_Button Pushed");
    Serial.println();
  }
  if (Up_Button.check() == LOW) {
    overall_menu.previous_screen();
    Serial.print("Up_Button Pushed");
    Serial.println();
  }
  if (Select_Button.check() == LOW) {
    overall_menu.call_function(increase);
    Serial.print("Select_Button Pushed");
    Serial.println();
  }
  if (Back_Button.check() == LOW) {
    overall_menu.call_function(decrease);
    Serial.print("Back_Button Pushed");
    Serial.println();
  }
  if (Spare_Button.check() == LOW) {
    overall_menu.switch_focus();
    Serial.print("Spare_Button Pushed");
    Serial.println();
  }
}
//Value Change Functions
//Water Functions
//Water Amount
//Increase Water Amount
void increase_water_amount() {
  if (water_amount < max_water_amount) {
    water_amount += increment_water_amount;
    Serial.print("Increase Water_Amount called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_amount, water_amount);
    Serial.print("Water Amount saved ");
    Serial.print(water_amount);
    Serial.println();
  } else {
    water_amount = max_water_amount;
    Serial.print("Max Water Amount reached");
    Serial.println();
  }
}

//Decrease Water Amount
void decrease_water_amount() {
  if (water_amount > min_water_amount) {
    water_amount -= increment_water_amount;
    Serial.print("Decrease Water Amount called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_amount, water_amount);
    Serial.print("Water Amount saved ");
    Serial.print(water_amount);
    Serial.println();
  } else {
    water_amount = min_water_amount;
    Serial.print("Min Water Amount reached");
    Serial.println();
  }
}

//Water Temperature
//Increase Water Temperature
void increase_water_temperature() {
  if (water_temperature < max_water_temperature) {
    water_temperature += increment_water_temperature;
    Serial.print("Increase Water_Temperature called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_temperature, water_temperature);
    Serial.print("Water Temperature saved ");
    Serial.print(water_temperature);
    Serial.println();
  } else {
    water_temperature = max_water_temperature;
    Serial.print("Max Water Temperature reached");
    Serial.println();
  }
}

//Decrease Water Temperature
void decrease_water_temperature() {
  if (water_temperature > min_water_temperature) {
    water_temperature -= increment_water_temperature;
    Serial.print("Decrease Water Temperature called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_temperature, water_temperature);
    Serial.print("Water Temperature saved ");
    Serial.print(water_temperature);
    Serial.println();
  } else {
    water_temperature = min_water_temperature;
    Serial.print("Min Water Temperature reached");
    Serial.println();
  }
}

//Water Flow
//Increase Water Flow
void increase_water_flow_rate() {
  if (water_flow_rate < max_water_flow_rate) { //this has to be evaluated, what is a feasible flow rate
    water_flow_rate += increment_water_flow_rate;     //what is a feasible increase / decrease value
    Serial.print("Increase Water_Flow_rate called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_flow_rate, water_flow_rate);
    Serial.print("Water Flow_Rate saved ");
    Serial.print(water_flow_rate);
    Serial.println();
  } else {
    water_flow_rate = max_water_flow_rate;
    Serial.print("Max Water Flow Rate reached");
    Serial.println();
  }
}

//Decrease Water Flow
void decrease_water_flow_rate() {
  if (water_flow_rate > min_water_flow_rate) {
    water_flow_rate -= increment_water_flow_rate;
    Serial.print("Decrease Water Flow Rate called");
    Serial.println();
    //Save Input
    EEPROM.put(ee_address_water_flow_rate, water_flow_rate);
    Serial.print("Water Flow_Rate saved ");
    Serial.print(water_flow_rate);
    Serial.println();
  } else {
    water_flow_rate = min_water_flow_rate;
    Serial.print("Min Water Temperature reached");
    Serial.println();
  }
}

// Used for attaching something to the lines, to make them focusable.
void fBlankFunction() {
  return;
}

//Function to switch to the Water Menu
void goto_mwater() {
  overall_menu.change_menu(mwater);
  Serial.print("Go To Water Menu");
  Serial.println();
}
//Callback function that will be attached to back_line to Main Menu
void mm_go_back() {
  overall_menu.change_menu(mmain);
  Serial.print("Go To Main Menu");
  Serial.println();
}

void setup() {

  //Start Serial Monitor and print the same text as on the LCD
  Serial.begin(9600);

  //Read the values recorded in the EEPROM
  //Water Values
  //Water Amount
  EEPROM.get(ee_address_water_amount, water_amount);
  Serial.print("The Water Amount stored on the EEPDROM is ");
  Serial.print(water_amount);
  Serial.println();
  //Water Temperature
  EEPROM.get(ee_address_water_temperature, water_temperature);
  Serial.print("The Water Temperature stored on the EEPDROM is ");
  Serial.print(water_temperature);
  Serial.println();
  //Water Flow Rate
  EEPROM.get(ee_address_water_flow_rate, water_flow_rate);
  Serial.print("The Water Flow Rate stored on the EEPDROM is ");
  Serial.print(water_flow_rate);
  Serial.println();

  //Start LCD Screen
  lcd.begin(16, 2);

  //Attaching a function to the lines is required for scrolling to work
  //Main
  main_line_1.attach_function(1, goto_mwater);
  mmback_line.attach_function(1, mm_go_back);

  //Water
  water_line_1.attach_function(increase, increase_water_amount);
  water_line_1.attach_function(decrease, decrease_water_amount);
  water_line_2.attach_function(increase, increase_water_temperature);
  water_line_2.attach_function(decrease, decrease_water_temperature);
  water_line_3.attach_function(increase, increase_water_flow_rate);
  water_line_3.attach_function(decrease, decrease_water_flow_rate);

  // Set the number of lines the display has
  //Main
  smain_1.set_displayLineCount(2);

  //Water
  swater_1.set_displayLineCount(2);

  mmain.add_screen(smain_1);
  mwater.add_screen(swater_1);

  overall_menu.update();
}

void loop() {
  buttonsCheck();
}

So after clearing the EEPROM and running the sketch above the Serial Monitor reads the following:

17:28:46.077 -> YThe Water Amount stored on the EEPDROM is 0

17:28:47.033 -> The Water Temperature stored on the EEPDROM is 0

17:28:47.099 -> The Water Flow Rate stored on the EEPDROM is 0

when pressing the buttons in series like you suggested it reads the following:

17:30:17.614 -> Down_Button Pushed

17:30:18.156 -> Up_Button Pushed

17:30:18.683 -> Select_Button Pushed

17:30:19.362 -> Back_Button Pushed

17:30:19.964 -> Spare_Button Pushed

When entering the "Water Set." Menu and increasing the values one by one:

17:31:23.621 -> Go To Water Menu

17:31:23.621 -> Select_Button Pushed

17:31:23.958 -> Select_Button Pushed

17:31:24.640 -> Spare_Button Pushed

17:31:26.095 -> Increase Water_Amount called

17:31:26.095 -> Water Amount saved 5

17:31:26.128 -> Select_Button Pushed

17:31:26.851 -> Spare_Button Pushed

17:31:27.397 -> Increase Water_Temperature called

17:31:27.430 -> Water Temperature saved 5

17:31:27.462 -> Select_Button Pushed

17:31:28.049 -> Spare_Button Pushed

17:31:28.761 -> Increase Water_Flow_rate called

17:31:28.794 -> Water Flow_Rate saved 5

17:31:28.794 -> Select_Button Pushed

When restarting the Arduino I get the following:

17:31:50.977 -> The Water Amount stored on the EEPDROM is 1285

17:31:51.042 -> The Water Temperature stored on the EEPDROM is 1285

17:31:51.108 -> The Water Flow Rate stored on the EEPDROM is 5

An int is 2 bytes, EEPROM stores data in bytes. Your EEaddresses increment by 1, so you are overlapping the data and corrupting it.

Thanks very much! Yes I didn't catch that one! You are correct, by increasing the ee_addresses it works now! :heart_eyes:

1 Like

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