Probably a simple question but i can't find the answer anywhere.
Looking at the examples from these pages.
I can see how it writes and reads values from multiple positions using
eeAddress += sizeof(float);
eeAddress = sizeof(float);
But what happens when trying to read if the sketch changes the size of the first value in eeprom from say two bytes to one?
Does that leave an empty byte between address1 and address2?
How is this handled?
Assuming the value in firstAddress is already 2 bytes. When the program sees the sensor value has changed, the value in firstAddress becomes 1 byte. secondAddress is still in the same position in eeprom but now there is an empty position in front of it.
firstAddress,secondAddress to firstAddress,empty,secondAddress
Yes. YOU are responsible for remembering where your data is. You are telling your sketch to store and fetch a byte from address 0. If you don't read or write address 1 then your sketch will ignore the address you no longer use.
However, if you change your setup to say secondAddress = sizeof(1byte);
the value of secondAddress will change and you will be getting that data item from the wrong address.
I think i follow now. So if secondAddress = sizeof(firstAddress) When the size of the firstAddress changes and you call the secondAddress it will skip the empty section in the eeprom
Those particular lines of code should probably not be in an example, because they can be a bit confusing. sizeof(float) returns the number of bytes needed to store a float. A float always takes the same number of bytes (for a given arduino board) regardless of the value stored in the float.
eeAddress was originally set to 0, where a float was then stored, so the next unused location is eeAddress + (number of bytes needed for float). It is more conventional to hardcode the eeprom addresses as constants instead of dynamically calculating in the code, even easier to use a struct for all the data being stored in eeprom.
I thought they wouldn't be ideal examples. I already have hardcoded what I'm working on into the eeprom, was just trying to get my head around whether this was a more efficient method. I hadn't thought of putting all the data into a struct though thanks.
Yes so regardless of whether the size of firstAddress changes from 2 bytes to 1, the program will always look for secondAddress in the same position it was when it was first compiled