I would like to be able to read eeprom addresses consecutively. I have the eeprom library. Can I do something like this; a,b,c =eeprom read(0,1,2). Where the values stored at these addresses are transferred to the 3 variables a,b,c.
If I can do this it will save a lot of programming space. What would be the correct coding for this?
cycle FOR
and for bunch of variables is array recommended, and when you be experienced programmer you create a structure which allow you to store and/or to read with one single method(line of code)
struct myStructure={
byte a;
byte b;
byte c;
}structure1;
EEPROM.put(address, structure1);
EEPROM.get(address, structure1);
Serial.print(structure1.a);
Serial.print(structure1.b);
Serial.print(structure1.c);
Depending on the data types, an ‘array’ might offer some ideas.
why do you think it would save "space"? (do you mean stuff to type in?)
A struct or an array might be the right approach. You need to clarify the types of a, b and c.
I am pretty sure that the library has some example-codes
no you have to assign each and every variable on its own with an extra line of code
or
use an array
for reading you will have to specify the adress from where you want to read.
Again. See the examples
What type of external EEPROM and the Library you are using?
Tell us which Arduino, please, and whether the EEPROM is built in, a separate device, or possibly even "emulated" for compatibility?
yes I must assign separately. I have a further problem with eeprom. before assigning any value to the eeprom I am getting a return of 255 which is the default values unless set to zero.
The suggested way to set all the addresses to zero is a write zero to all the eeprom. The problem here is that this is done in setup. thus every time you close and reopen the serial monitor you cannot save a variable because every thing is set back to zero.
There must be another way to set all of eeprom to zero values.
Could it be done before setup?
I see a way now to set eeprom to zero. program first with the iteration in setup. upload this to the board. then delete the section in setup that sets eeprom to zero and upload again to the board with new data.
i did the same thing with the real time clock. set the clock and upload. then delete the part that sets the clock and upload again. this way you are not resetting the clock every time you use the serial monitor.
Use an eeprom location outside of the area used for abc .
In setup:
Read the value in that location , if it’s 255 clear all the eeprom
In setup write a value to that location .
Next time the program runs , that location will contain the value , so don’t clear the rest of eeprom .
You can use the same method to load default values , if you wanted .
Use something based on https://wiki-content.arduino.cc/en/Tutorial/LibraryExamples/EEPROMCrc.
You calculate the CRC over the eeprom contents except for the last 4 bytes. The last 4 bytes will contain a CRC that you calculate. Compare that against the last 4 bytes.
First time that you run the sketch, the CRC will more than likely fail. In that case you clear the EEPROM, calculate the CRC and store in in the last 4 bytes.
Every time that you write to the EEPROM, you recalculate the CRC and store it again.
Your test in setup can be used to check for EEPROM corruption.
but why would you need to set everything to 0 ?
255 is as good as 0 as a random, non initialized value
the typical way we deal with EEPROM is like others have said. You define a specific address in EEPROM (4 bytes for example) that you use as a "sentinel".
At the start of the setup(), you check the content of the sentinel area.
➜ if the content is the keyword — say 0xDEADBEEF — then it's very likely that the data was already initialized and you can read safely the rest of the EEPROM to initialise your variables to whatever was saved.
➜ if the content is not the keyword then your EEPROM has not been initialised yet, so you initialise it with your starting default values and set the keyword in the sentinel area for next time.
All the locations in an EEPROM contain 0xff when the EEPROM is blank because it is inherent in the way an EEPROM works that this is the case. I can see no reason to change them to 0x00. Why do you want to change them?
You mean when the EEPROM is erased, it is said that EEPROM is in blank state and its all locations should contain FFs..