Hi,
I'm struggling to understand why I cant use an array of structure to save data to EEPROM . I have copied the put and get examples from reference into one sketch, and this worked fine. I then tried to create an array of MyObject by changing customVar to an array, and then (for test) using a '0' element to repeat
/***
Written by Christopher Andrews 2015
Released under MIT licence.
Modified stevewidg with put/get examples in 1 File, then use an array of MyObject
***/
#include <EEPROM.h>
struct MyObject {
float field1;
byte field2;
char name[10];
};
struct MyObject customVar[5];
float f = 123.456f; //Variable to store in EEPROM.
int i = 0;
void setup() {
Serial.begin(9600);
InitialiseEEPROM();
Serial.print("Read float from EEPROM: ");
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get(0, f);
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
secondTest(); //Run the next test.
}
void secondTest() {
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
// This will need to be modified in for loop to make address index*sizeof(customVar) +eeaddress
// just use 0 for now
// customVar[0] //Variable to store custom object read from EEPROM.
EEPROM.get(eeAddress,customVar[0]);
Serial.println("Read custom object from EEPROM: ");
Serial.println(customVar[0].field1);
Serial.println(customVar[0].field2);
Serial.println(customVar[0].name);
}
void InitialiseEEPROM() {
int eeAddress = 0; //Location we want the data to be put.
//One simple call, with the address first and the object second.
EEPROM.put(eeAddress, f);
Serial.println("Written float data type!");
//Data to store. CustomVar[x] has been defined
MyObject customVar[0] = {
3.14f,
65,
"Working!"
};
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.put(eeAddress,customVar[0]);
Serial.println("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
Serial.println(customVar[0].field1);
Serial.println(customVar[0].field2);
Serial.println(customVar[0].name);
}
void loop() {
/* Empty loop */
}
Ultimately want an array of 6 items that can be accessed globally. I have searched this, and other forums, but not found a solution using put/get commands for eeprom.
Thanks
Thanks JML,
thats an unbelievably quick reply, which I guess means that the answer was obvious, but its only obvious when you know the answer!!
You are correct my program didnt compile, but I couldnt figure why not. I'll try be more specific in future. Anyway your solution didnt appear to work for the strcpy function until I made it a pointer *strcpy. There isnt a lot of information on this function on the forum, and my solution is based mostly on trial and error. Would really appreciate if someone could tell me why this appears to work, or is there an alternative?
There isnt a lot of information on this function on the forum, and my solution is based mostly on trial and error.
strcpy is normal C stuff. This site tends to focus on the beginner stuff and the stuff specific to Arduino. They tend to assume that you can find the C++ stuff on the internet.
Try googling "strcpy" and I think you will find plenty on how that works.
The only place you get to use a brace enclosed initializer list like that is on the same line where you first create the object. Anywhere else in code you have to do things one piece at a time.
stevewidg:
Anyway your solution didnt appear to work for the strcpy function until I made it a pointer *strcpy. There isnt a lot of information on this function on the forum, and my solution is based mostly on trial and error. Would really appreciate if someone could tell me why this appears to work, or is there an alternative?
this is clearly not MY solution but YOURS... I told you you can’t initialise a structure that way... you need to go element by element .... I showed you the code... why didn’t you use it?
Thanks for your replies guys, really appreciated,
I realised within milliseconds of sending that I should have said I had checked on the web for strcpy, and got myself confused by not reading JML's answer properly. and also strcpy seems to use * a lot. A stupid newbie mistake of not realising that a ',' and ';' are different. Again its obvious when you know the answer. So thank you for your replies, I'll read replies more carefully and try to take it to the next level, and no doubt be back.