This is an old code i wrote when i was trying to figure out how to save structs to the EEPROM, i never worked out how to save arrays without listing every cell in the put() and get() but it will at least help you understand the basics of structs and EEPROM. And sure you can have an arrays of structs, and even an array of structs which point to other structures like i do with font engines.
#include <EEPROM.h>
byte EEPROM_key = 101; // change this key if you adjust the structure of the EEPROM ! i.e 102
// simple variables to store
bool test_1 = true;
char test_2 = '!';
byte test_3 = 101;
int test_4 = 1234;
long test_5 = 1234567;
float test_6 = 21.43;
// arrays to store
bool test_array_1[5] = {true, false, true, false, true};
char test_array_2[5] = {'T', 'e', 's', 't', '1'};
byte test_array_3[5] = {101, 102, 103, 104, 105};
int test_array_4[5] = {1234, 1235, 1236, 1237, 1238};
long test_array_5[5] = {1234567, 1234568, 1234569, 1234570, 1234571};
float test_array_6[5] = {21.43, 21.44, 21.45, 21.46, 21.47};
struct EEPROMstructure {
bool EEPROM_test_1;
char EEPROM_test_2;
byte EEPROM_test_3;
int EEPROM_test_4;
long EEPROM_test_5;
float EEPROM_test_6;
// arrays
bool EEPROM_test_array_1[5];
char EEPROM_test_array_2[5];
byte EEPROM_test_array_3[5];
int EEPROM_test_array_4[5];
long EEPROM_test_array_5[5];
float EEPROM_test_array_6[5];
};
void setup() {
Serial.begin(115200);
Serial.println("EEPROM structure example v1");
readEEPROM();
printvalues(); // show retrived EEPROM data
}
void loop() {
// nothing going on here
}
void readEEPROM(void) {
if (EEPROM_key == EEPROM.read(0)) {
retriveEEPROM();
}
else {
Serial.println(F("EEPROM was blank or the structure changed, but is now corrected"));
EEPROM.put(0, EEPROM_key);
writeEEPROM();
}
}
void writeEEPROM(void) {
Serial.println(F("wrote variables to eeprom"));
EEPROMstructure writeEEPROM = {
test_1,
test_2,
test_3,
test_4,
test_5,
test_6,
test_array_1[0], // array 1
test_array_1[1],
test_array_1[2],
test_array_1[3],
test_array_1[4],
test_array_2[0], // array 2
test_array_2[1],
test_array_2[2],
test_array_2[3],
test_array_2[4],
test_array_3[0], // array 3
test_array_3[1],
test_array_3[2],
test_array_3[3],
test_array_3[4],
test_array_4[0], // array 4
test_array_4[1],
test_array_4[2],
test_array_4[3],
test_array_4[4],
test_array_5[0], // array 5
test_array_5[1],
test_array_5[2],
test_array_5[3],
test_array_5[4],
test_array_6[0], // array 6
test_array_6[1],
test_array_6[2],
test_array_6[3],
test_array_6[4],
};
EEPROM.put(1, writeEEPROM);
delay(250);
}
void retriveEEPROM(void) {
Serial.println(F("retriving EEPROM variables"));
EEPROMstructure retriveEEPROM;
EEPROM.get(1, retriveEEPROM);
delay(250);
test_1 = retriveEEPROM.EEPROM_test_1;
test_2 = retriveEEPROM.EEPROM_test_2;
test_3 = retriveEEPROM.EEPROM_test_3;
test_4 = retriveEEPROM.EEPROM_test_4;
test_5 = retriveEEPROM.EEPROM_test_5;
test_6 = retriveEEPROM.EEPROM_test_6;
test_array_1[0] = retriveEEPROM.EEPROM_test_array_1[0]; // array 1
test_array_1[1] = retriveEEPROM.EEPROM_test_array_1[1];
test_array_1[2] = retriveEEPROM.EEPROM_test_array_1[2];
test_array_1[3] = retriveEEPROM.EEPROM_test_array_1[3];
test_array_1[4] = retriveEEPROM.EEPROM_test_array_1[4];
test_array_2[0] = retriveEEPROM.EEPROM_test_array_2[0]; // array 2
test_array_2[1] = retriveEEPROM.EEPROM_test_array_2[1];
test_array_2[2] = retriveEEPROM.EEPROM_test_array_2[2];
test_array_2[3] = retriveEEPROM.EEPROM_test_array_2[3];
test_array_2[4] = retriveEEPROM.EEPROM_test_array_2[4];
test_array_3[0] = retriveEEPROM.EEPROM_test_array_3[0]; // array 3
test_array_3[1] = retriveEEPROM.EEPROM_test_array_3[1];
test_array_3[2] = retriveEEPROM.EEPROM_test_array_3[2];
test_array_3[3] = retriveEEPROM.EEPROM_test_array_3[3];
test_array_3[4] = retriveEEPROM.EEPROM_test_array_3[4];
test_array_4[0] = retriveEEPROM.EEPROM_test_array_4[0]; // array 4
test_array_4[1] = retriveEEPROM.EEPROM_test_array_4[1];
test_array_4[2] = retriveEEPROM.EEPROM_test_array_4[2];
test_array_4[3] = retriveEEPROM.EEPROM_test_array_4[3];
test_array_4[4] = retriveEEPROM.EEPROM_test_array_4[4];
test_array_5[0] = retriveEEPROM.EEPROM_test_array_5[0]; // array 5
test_array_5[1] = retriveEEPROM.EEPROM_test_array_5[1];
test_array_5[2] = retriveEEPROM.EEPROM_test_array_5[2];
test_array_5[3] = retriveEEPROM.EEPROM_test_array_5[3];
test_array_5[4] = retriveEEPROM.EEPROM_test_array_5[4];
test_array_6[0] = retriveEEPROM.EEPROM_test_array_6[0]; // array 6
test_array_6[1] = retriveEEPROM.EEPROM_test_array_6[1];
test_array_6[2] = retriveEEPROM.EEPROM_test_array_6[2];
test_array_6[3] = retriveEEPROM.EEPROM_test_array_6[3];
test_array_6[4] = retriveEEPROM.EEPROM_test_array_6[4];
}
void printvalues() {
Serial.print(F("test_1 = ")); Serial.println(test_1);
Serial.print(F("test_2 = ")); Serial.println(test_2);
Serial.print(F("test_3 = ")); Serial.println(test_3);
Serial.print(F("test_4 = ")); Serial.println(test_4);
Serial.print(F("test_5 = ")); Serial.println(test_5);
Serial.print(F("test_6 = ")); Serial.println(test_6);
for (bool val : test_array_1) {val == true ? Serial.print(F("true, ")) : Serial.print(F("false, "));} Serial.println();
for (char val : test_array_2) {Serial.print(val); Serial.print(F(", "));} Serial.println();
for (byte val : test_array_3) {Serial.print(val); Serial.print(F(", "));} Serial.println();
for (int val : test_array_4) {Serial.print(val); Serial.print(F(", "));} Serial.println();
for (long val : test_array_5) {Serial.print(val); Serial.print(F(", "));} Serial.println();
for (float val : test_array_6) {Serial.print(val); Serial.print(F(", "));} Serial.println();
}