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();
}