I need help getting introduced to EEPROM. So I have written a very basic code to get me started.
My code utilizes two buttons and a LCD screen. I need someone to complete it.
The purpose of the code is to count the number of times the "add" button is pressed and display it on the LCD screen.
When the "save" button is pressed I want it to save the "tally" so I can unplug the arduino.
Then when I plug in the arduino I can continue with the "tally"
Code Below,
#include <LiquidCrystal.h> #include <EEPROM.h>
LiquidCrystal lcd(0, 1, 8, 9, 10, 11);
const int Pin_Button_Add = 4;
const int Pin_Button_Save = 7;
int Button_Add = 0;
int Button_Save = 0;
int Tally_Counter = 0;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
as already mentioned, you will need to retrieve the value stored in EEPROM in the setup. EEPROM.get
no need for splitting of a variable in single bytes when you use put and get - will be done by the right methods.
untested but compiles without warnings:
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(0, 1, 8, 9, 10, 11);
const int Pin_Button_Add = 4;
const int Pin_Button_Save = 7;
byte Button_Add = 0; // only HIGH/LOW - byte or bool is enough
byte Button_Save = 0;
uint16_t Tally_Counter = 0; // unsigned for a counter
const byte addrCounter = 0; // an adress in the eeprom.
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(Pin_Button_Add, INPUT);
pinMode(Pin_Button_Save, INPUT);
//EEPROM.put(addrCounter, Tally_Counter); // Reset the EEPROM one time - upload the sketch once with this line active - it will write 0 to the given EEPROM address
EEPROM.get(addrCounter, Tally_Counter);
}
void loop() {
Button_Add = digitalRead(Pin_Button_Add);
lcd.print(Tally_Counter);
if (Button_Add == HIGH) {
Tally_Counter++;
delay(200);
}
if (Button_Save == HIGH) {
EEPROM.put(addrCounter, Tally_Counter);
delay(200);
}
}
Don't forget EEPROM addresses hold bytes so if you have larger numbers, you need to split the number into bytes.
Or use EEPROM.put() which saves data of any type and its companion EEPROM/get() which loads any data type.
NOTE that different data types need different numbers of bytes in which to save the data so you need to manage the EEPROM address to which you write with that in mind
1. There is 1 KByte (1024 locations; where each location holds 1-byte/8-bit data) non-volatile EEPROM (Electrical Erasable Electrically Programmable Random Access Read Only Memory) inside ATmega328P MCU. The memory locations have their own addresses, and these are: 0x0000, 0x0001, ..., 0x03FF. The conceptual view of the EEPROM is shown in Fig-1. To store 1-byte data in a memory location, a 'write time of about 5 ms' is needed. Reading data from an EEPROM location is almost instantaneous.
Figure-1:
2. To write 1-byte data (say, 0x23) at location 0x0001, we execute the following steps: (1) Select the target memory location with the help of EEPROM address register. (2) Place data at the target location using EEPROM data register. (3) Issue data write command using EEPROM control register. (4) Wait for 5 ms period for the data byte to get written into the target location or keep checking that data write cycle is complete using EEPROM status register.
3. All the sub-steps of Step-2 are automatically done when we execute the following instruction of the EEPROM.h Library:
4. To store the 16-bit content of the 'Tally_Counter' variable into two consecutive memory locations (0x0000 andd 0x0001), we may execute instruction(s) of one of the following two groups.
EEPROM.write(0x0000, lowByte(Tally_Counter));//if Tally_Counter=0x1234, then lowByte(Tally_Counter)=0x34
EEPROM.write(0x0001, highByte(Tally_Counter));
OR
EEPROM.put(0x0000, Tally_Counter); //lower byte of Tally_Counter will be stored at location 0x0000
5. Exercise (1) Write codes using EEPROM.write() instruction to save 12.57 into EEPROM starting at location 0x0010. (2) Repeat Step-5.1 using EEPROM.put() instruction.