I started out by blindly copying the put and get commands from an example as:
put( 0, variablen) and get ( 0, variablen)
These worked just fine for my first three variables - all of type byte.
Now I've added some additional variable, mostly of type int, and the results are incorrect. The gets seem to retrieve the wrong variables.
After some research, I found that the first variable (which I always called 0, but did not know why - except that it worked) was the address in EEPROM.
It seems that the system made an internal table to keep track of where the variables are stored. (Different variable types require different numbers of bytes for address storage.)
I can make up my own table, but of course that is more messy.
What am I doing wrong?
Am I asking too much of the system?
Any further help in understanding how this works will be appreciated.
- Matt
Well, first, what are you doing? You left no code or real explanation.
Example:
Now I've added some additional variable, mostly of type int, and the results are incorrect. The gets seem to retrieve the wrong variables.
What variable? What type were the other variables? What were the results? Why do you think the get function retrieve the wrong variables?
See the problem?
Why do you think the EEPROM code should magically manage the variable locations for you? You're supposed to tell it where to put the data. Read the documentation, please!
I read the documentation - after I started having problems.
The first three values I stored were all "bytes" and all went well.
After I started having problems, and after reading the documentation, I realized that the put and get lines should include the addresses, rather than always be zero.
However, the system has enough information as to the variable type that it COULD "magically" make and keep a table as to where each variable is stored. I can do this, but it seems that the system could do it more easily, and it seemed to be doing so for the first few variables.
If I am wrong, I can keep a table of the variable addresses and use this, but I suspect that I might not have to, if I only know the right "magic words!"
Were you giving the variables different numbers?
EEPROM.put(0, firstByteVariable);
EEPROM.put(1, secondByteVariable);
EEPROM.put(2, thirdByteVariable);
That would work forever, for variables that fit in one byte.
If you want the compiler to keep track of where each variable is, you can put them all in a structure:
struct EEPROMVariables {
byte firstByteVariable;
byte secondByteVariable;
byte thirdByteVariable; } eepromVars;
EEPROM.put(0, eepromVars); // Write all variables
EEPROM.get(0, eepromVars); // Read all variables
The variables would be read and written as a block and the compiler will keep track of where in the block each variable is. You would have to use their full name: "eepromVars.firstByteVariable" to use them.
The EEMEM characteristic of this standard C library does what you want, but the complexity of using it instead of the Arduino EEPROM.h with .put() and .get() will likely make you think that keeping track of the addresses is an easier task.
<avr/eeprom.h>: EEPROM handling
https://teslabs.com/openplayer/docs/docs/prognotes/EEPROM%20Tutorial.pdf