Hi,
I am using Arduino UNO, is it possible to write to EEPROM more complicated structures than only key/value ? For example I would like to write that structure:
MyStruct[][]
struct MyStruct
{
int age
short somedate;
string status;
};
Hi,
I am using Arduino UNO, is it possible to write to EEPROM more complicated structures than only key/value ? For example I would like to write that structure:
MyStruct[][]
struct MyStruct
{
int age
short somedate;
string status;
};
arduHenry:
I am using Arduino UNO, is it possible to write to EEPROM more complicated structures than only key/value ?
The EEPROM of an UNO is 1024 bytes in size.
You can write anything into it, which fits in size.
arduHenry:
For example I would like to write that structure:MyStruct[][]
struct MyStruct
{
int age
short somedate;
string status;
};
Declare some kind of bullshit, then save to EEPROM?
Sorry, that cannot work.
Besides of a missing semicolon after 'age', what the hell is "string"?
Is it a 'struct' or some type of data you declared before?
Did you perhaps mix small and capital letter and wanted to refer to a "String" object? String objects are pointers, you cannot just save a pointer. After restorung a pointer you would just have a pointer and not the contents of the original object. So you better forget about "String" objects.
Or maybe you wanted to declare a string, which is a char-array in the C language and might work like that:
struct MyStruct
{
char name[20]; // provide space for names up to a length of 20
int age;
short somedate;
};
Something like that?
Or what?
You can use the avr-gcc functions to read and write the EEPROM, and use a struct for the offset of the items by declaring the struct as EEMEM: EEMEM - #5 by Peter_n - Storage - Arduino Forum
Or use a "anything" library : Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino (search that page for "I2C_Anything").
template<class T>
int writeToEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
}
temp;
temp.type = value;
for (unsigned int i = 0; i < sizeof(T); i++)
{
EEPROM.write(address + i, temp.b[i]);
}
return sizeof(T);
}
template<class T>
int readFromEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
}
temp;
for (unsigned int i = 0; i < sizeof(T); i++)
{
temp.b[i] = EEPROM.read(address + i);
}
value = temp.type;
return sizeof(T);
}