Hi.
I have an Arduino that needs to store some data. The data I have is written in a struct like so:
struct RecordValues {
unsigned int a;
unsigned int b;
unsigned long c;
unsigned long d;
unsigned int e;
unsigned int f;
boolean g;
};
I have looked into the EEPROM library and it is exactly what I need.
I can write the initial default values fine and I can read them back with EEPROM.write and EEPROM.read respectively.
Now the question is how do I use the update function of the EEPROM.update with my structure.
I want to use the update function since I want to try to not write values that have not changed both because of time (3.3 ms each write) and because of the 100000 write limit.
Here is the code I use to read, write, and update. The update method is the one I am trying to figure out:
struct RecordValues readRecords(int eepromLocationToRead)
{
RecordValues readVar;
EEPROM.get(eepromLocationToRead, readVar);
return readVar;
}
void updateRecords(int eepromLocationToUpdate, struct RecordValues updatedValues)
{
//RecordValues readVar = readRecords(eepromLocationToUpdate);
//EEPROM.update(eepromLocationToUpdate, struct updatedValues);
}
void writeRecordsInitiallyWithDefaultValues(int eepromLocationToWrite)
{
RecordValues customVar = {
0,0,0,0,300,50,false
};
EEPROM.put(eepromLocationToWrite, customVar);
//Serial.println("Done writing.. commencing reading..");
}
If there is no easy solution then I guess I am just going to have to break the structure into individual variables and have an address associated with each one. But that is what I want to try to avoid.
Anyone?
you can use lower level eeprom code in
#include <avr/eeprom.h>
look at eeprom_read_block and eeprom_write_block
you can calculate the exact location (address) of the variable you want to update in your structure.
fwiw, I use these functions, but I always update the entire structure, granted I don't update very frequently.
If you look at eeprom.h, there are other functions to read/write word, dword, float, etc.
doughboy:
you can use lower level eeprom code in
#include <avr/eeprom.h>
I was really dreading an answer like this
Not that it is not helpful, it is just something that I would like to avoid.
I will see if there is someone with another solution
any answer you get will be exactly the same in principle, whether you use the high level code or low level code. You can apply the same principle on the high level EEPROM library function.
basically, calculate the exact address and write the data.
a function is a function, I don't get why one would be more dreaded than another.
doughboy:
a function is a function, I don't get why one would be more dreaded than another.
Haha true. But a function that is already made is better than one I have to make myself. Which is why I was hoping the update function could have been used with my struct.
On a side note. Is this calculation correct (how many bytes I am using up)?
simmisj:
struct RecordValues {
unsigned int a;
unsigned int b;
unsigned long c;
unsigned long d;
unsigned int e;
unsigned int f;
boolean g;
};
a = 2B
b = 2B
c = 4B
d = 4B
e = 2B
f = 2B
g = 1B?
total = 17B?
You probably need to step-back and re-evaluate your approach... Do you really need to write to non-volatile memory so frequently? If so, maybe an SD card or battery backed-up RAM is a better choice. Or, battery back-up the whole Arduino and save to EEPROM infrequently or when the battery gets low.
One write per hour for 10 years is less than 100,000 and if you have to write to EEPROM more than once a day it might be time to think about "plan-B".
I will explain a bit more what I am doing.
I have made a Conways game of life on a 16x16 LED matrix and the data that I want to store is for example the highest number of generations made before stagnation. Another example, the number of generations calculated since forever.
DVDdoug:
Do you really need to write to non-volatile memory so frequently?
No not really but if I do then there is less chance of lost data.
DVDdoug:
If so, maybe an SD card or battery backed-up RAM is a better choice. Or, battery back-up the whole Arduino and save to EEPROM infrequently or when the battery gets low.
Not an option for me. The hardware is up and I did not make plans for storage. It could be done sure, but I don't want to add that.
DVDdoug:
One write per hour for 10 years is less than 100,000 and if you have to write to EEPROM more than once a day it might be time to think about "plan-B".
Yes, exactly.
I have now started thinking about writing once every 10 minutes. That gives me 6 * 24 * 365 = 52560 writes per year. Then the plan is to keep a track of how many writes I make to switch to another location after some many thousands of writes.
Also, I think I will implement "smart" writing in some sense. Most of the time it will just sit on my desk plugged in. When I connect my phone to it to change the settings I will make it write the current settings first. That way I can maybe set the frequency of writing data to once a day just as a backup