Hie! I'm new to Arduino and programming, I'm working on a project and part of it requires that it keeps track of a users water intake on current day. I wanted it for first time of the day to prompt the user to enter amount of water taken (glasses of water) then after the input it keeps the amount and update it the next time the user takes an extra glass and inputs it. So every time the user will be reminded of their water intake so that they may keep track of how much more water they would wanna take on that day. I tried writing the code below but the EEPROM doesn't seem to be holding the first data I enter. How best can I write this code to work in the way I just explained. Any help on improvement or a better code will be highly appreciated.
#include <EEPROM.h>
int waterCount = 0;
void setup() {
Serial.begin(9600);
waterCount = EEPROM.read(0);
Serial.print("Number of glasses taken earlier today: ");
Serial.print(waterCount);
}
void loop() {
Serial.println("Enter the number of glasses taken recently (0-255): ");
while (Serial.available() ==0){
}
int userInput = Serial.parseInt();
if(userInput >= 0 && userInput <= 255){
waterCount = userInput;
Serial.print("Total number of glasses taken today: ");
Serial.println(waterCount);
EEPROM.write(0, waterCount);
} else{
Serial.println("Invalid input. please enter a number between 0 and 255.");
}
delay(2000);
}
What value does a not initiated EEPROM give?
Maybe You need a first time initiating code setting the value to zero?
Use Serial.print and serial monitor to check the readings!
Know that the EEPROM has a limited number of writings. 500, 1000..... What ever, it's limited.
'int waterCount = 0;' requires two bytes of storage. EEPROM.write/read only accesses a single byte - so you're storing/retrieving just half of your variable. Use EEPROM.get/put instead.
The best choice would be to flip a coin. They are not initialized from the factory but depending on the tests they have been through it is about the same as the coin will give you. I have seen mainly 0s and 0xffs but not always. If it has been used the last value will be there. If you want it defined do as Railroader suggests.
Do you want the following kind of interactive session?
Number of glasses taken earlier today: 0
Enter the number of glasses taken recently (0-255):
Total number of glasses taken today: 12
Total number of glasses taken upto now: 12
====================================================
Enter the number of glasses taken recently (0-255):
Total number of glasses taken today: 9
Total number of glasses taken upto now: 21
====================================================
Enter the number of glasses taken recently (0-255):
Do you want the codes? These are here (your sketch, but is slightly adjusted):
#include <EEPROM.h>
int waterCount = 0;
void setup()
{
Serial.begin(9600);
EEPROM.write(0, 0); //inital value at location 0x0000
waterCount = EEPROM.read(0);
Serial.print("Number of glasses taken earlier today: ");
Serial.println(waterCount);
}
void loop()
{
Serial.println("Enter the number of glasses taken recently (0-255): ");
while (Serial.available() == 0) {}
int userInput = Serial.parseInt();
if (userInput >= 0 && userInput <= 255)
{
waterCount = userInput;
Serial.print("Total number of glasses taken today: ");
Serial.println(waterCount);
byte y = EEPROM.read(0);
EEPROM.write(0, y + waterCount);
//----------------
Serial.print("Total number of glasses taken upto now: ");
Serial.println(EEPROM.read(0));
Serial.println("====================================================");
}
else
{
Serial.println("Invalid input. please enter a number between 0 and 255.");
}
delay(2000);
}
Thank you so much, the code its working how I wanted it to except for one thing, I lose the data stored once I shut off the arduino IDE, like if I open the arduino IDE after closing it I get the reading from the EEPROM as 0 instead of the last number I stored. How can I go about this?.... I wanted to store the data for the whole day even after a series of closing and opening the arduino IDE or disconnecting and reconnecting the arduino board(UNO R3).
#include <EEPROM.h>
int waterCount = 0;
void setup()
{
Serial.begin(9600);
EEPROM.write(0, 0); //inital value at location 0x0000
waterCount = EEPROM.read(0);
Serial.print("Number of glasses taken earlier today: ");
Serial.println(waterCount);
}
void loop()
{
Serial.println("Enter the number of glasses taken recently (0-255): ");
while (Serial.available() == 0) {}
int userInput = Serial.parseInt();
if (userInput >= 0 && userInput <= 255)
{
waterCount = userInput;
Serial.print("Total number of glasses taken today: ");
Serial.println(waterCount);
byte y = EEPROM.read(0);
EEPROM.write(0, y + waterCount);
//----------------
Serial.print("Total number of glasses taken upto now: ");
Serial.println(EEPROM.read(0));
Serial.println("====================================================");
}
else
{
Serial.println("Invalid input. please enter a number between 0 and 255.");
}
delay(2000);
}
But how to ensure that the target location (0x0000) of the EEPROM intially contains 0 (zero) -- I mean that the location 0x0000 should have zero befor the sketch of post #11 is executed without EEPROM.write(0, 0) instruction in the setup() function?
Use two eeprom locations. We'll call them today and total.
First message in Setup could be "Enter NEW if starting a new day. Press Enter to continue". Reset today if NEW is entered.
Accumulate all counts in total. Eventually you may want to reset total. Maybe enter reset to rest all.