EEPROM_read/writeAnything questions

Hi all,

I'm trying to use the functions to read and write data from the EEPROM using what is described here: Arduino Playground - EEPROMWriteAnything.

Oh well, i'll include the code anyway:

#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;
}

Can someone point me in the right direction (code wise) on how to save/read the following data:

boolean initialConfig = 0;
double P1[7]={21,21,21,21,21,16,16};
double P2[7]={20,20,20,20,20,20,20};
double P3[7]={21,21,21,21,21,21,21};
double P4[7]={16,16,16,16,16,16,16};
long T1[7]={23400,23400,23400,23400,23400,30600,30600}; // 6:30 time in seconds since start of day at 0:00:00
long T2[7]={30600,30600,30600,30600,30600,30600,30600}; // 8:30
long T3[7]={72000,72000,72000,72000,72000,72000,72000}; // 20:00
long T4[7]={84600,84600,84600,84600,84600,84600,84600}; // 23:30

If i understood it well, the P1,P2,P3,P4 contain all 1-byte values, while T1,T2,T3,T4 contain 2-byte values, right?

So the following code should work i guess: (not tried yet)

void setup() {
  Serial.begin(9600);
  EEPROM_readAnything(0, initialConfig);  //check if there is already a config in the EEPROM (address 0 should be 1)
  if (initialConfig == 0) saveInitialConfig();
}

void saveInitialConfig() {
  initialConfig = 1;
  int bytesWritten = EEPROM_writeAnything(0, initialConfig);
  bytesWritten += EEPROM_writeAnything(bytesWritten, P1);
  bytesWritten += EEPROM_writeAnything(bytesWritten, P2);
  bytesWritten += EEPROM_writeAnything(bytesWritten, P3);
  bytesWritten += EEPROM_writeAnything(bytesWritten, P3);
  bytesWritten += EEPROM_writeAnything(bytesWritten, T1);
  bytesWritten += EEPROM_writeAnything(bytesWritten, T2);
  bytesWritten += EEPROM_writeAnything(bytesWritten, T3);
  bytesWritten += EEPROM_writeAnything(bytesWritten, T4);

}

But afterwards, how do I know where I should start reading the EEPROM to get for ex. T2 ?

If there are better ways to accomplish this, I'm happy to hear it :wink:

Greetings,
EriSan500

If i understood it well, the P1,P2,P3,P4 contain all 1-byte values, while T1,T2,T3,T4 contain 2-byte values, right?

P1 is a double, which, on the Arduino, is the same size as a float - 4 bytes.
T1 is a long - also 4 bytes.

The reason for using templates in the definition of the readAnything and writeAnything methods is so that the methods can determine the size of the data being written/read.

You are writing the data correctly. As long as you read it in the same order, all will be well. If you need to read it as random access, you'll need to compute the correct offset for each variable. Use the sizeof function to be sure that you get the sizes right.