Overview
EEPROMStore
is an Arduino library to look after saving and loading data from the EEPROM. Data in the EEPROM is not lost when the device loses power, or is reset, so it is a good place for storing program configuration. A check sum is stored with your data so the library can detect an EEPROM that hasn’t been initialized, or may contain invalid data. The EEPROMStore
is part of the MegunoLink Arduino library.
The EEPROMStore
works with data that lives in a structure. A structure is one kind of complex type. But it isn’t very complicated; a complex type is just a collection of simple types. And a simple type is something like an int, or long, or a byte. The structure groups them together so we can refer to them as a single thing.
Here’s a simple structure that has two members: temperature and humidity set points:
1 2 3 4 5 |
struct GreenhouseControllerConfiguration { int TemperatureSetPoint; int HumiditySetPoint; }; |
A struct
is used for the EEPROMStore
to make it easy to load and save a group of values all together.
A structure used with the EEPROMStore
must also define a reset method. The reset method is called by the library whenever it detects invalid data. Typically this occurs the first time your sketch is run…because you haven’t saved any data at that point. The Reset
method gets called to initialize all the variables in the structure.
So, to store a humidity and temperature set-point for a green house controller, we might declare a structure like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct GreenhouseControllerConfiguration { int TemperatureSetPoint; int HumiditySetPoint; void Reset() { // Set default values in case there is nothing // stored in the eeprom yet. TemperatureSetPoint = 10; HumiditySetPoint = 80; } }; |
Storing the configuration in the data structure is then simply:
1 2 3 |
#include "EEPROMStore.h" EEPROMStore<GreenhouseControllerConfiguration> Configuration; |
Configuration values are accessed through the Data
property. Previously stored values are loaded automatically when the device is reset. Call the Save
method to update the EEPROM when values are changed.
1 2 3 4 5 |
Serial.print("Temperature set-point: "); Serial.println(Configuration.Data.TemperatureSetPoint); Configuration.Data.TemperatureSetPoint = 30; Configuration.Save(); |
EEPROMStore Reference
The EEPROMStore
is a template that takes a single argument: the name of the structure type to store. You’ll also need to include the library header file to use it:
1 2 3 |
#include "EEPROMStore.h" EEPROMStore<GreenhouseControllerConfiguration> Configuration; |
The last saved value will be loaded automatically when your sketch starts. If there is no previously saved value (because this is the first time the sketch has been run) the default value is set using the Reset()
function.
Data Property
The EEPROMStore
variable has a Data
property. With the type of the structure you’re storing. You could print the current values using something like this:
1 2 3 4 5 6 7 8 |
void PrintConfiguration() { Serial.print("Temperature set-point: "); Serial.println(Configuration.Data.TemperatureSetPoint); Serial.print("HumiditySetPoint set-point: "); Serial.println(Configuration.Data.HumiditySetPoint); } |
And set a new value like this:
1 2 |
Configuration.Data.TemperatureSetPoint = 12; Configuration.Data.HumiditySetPoint = 22; |
Changing the value in the Data
member doesn’t change the value stored in the EEPROM. You need to call the Save
method.
Saving
To save the current value of data to the eeprom, call the Save
method.
1 |
Configuration.Save(); |
Loading
To overwrite the current value of the data with the value last saved in the EEPROM, call the Load
method:
1 |
Configuration.Load(); |
Reset
To replace the current value of the data with the default value, call the Reset
method:
1 |
Configuration.Reset(); |
This doesn’t update the value in the EEPROM however. Call the Save
method to do that.