EEPROM Read Values

Hi,

Let’s take the following code and look at its output to the serial. On one hand, we have millis() assigned to time and it is printing out to the serial monitor every second give or a take some milliseconds. On the other hand, that millis() int value is being written via EEPROM to memory address 0, but reading EEPROM at address 0 outputs a different value to the serial monitor than what is expected. Why is this so?

#include <EEPROM.h>
int time; 
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  time=millis();
  Serial.println(time);
  EEPROM.write(0,time);
  Serial.println(EEPROM.read(0));
  delay(1000);
}

millis() returns an unsigned long of raw hex data. You are capturing it using an int. So two bytes are lost. Then storing that as a byte. Another byte is lost. Then sending that as data that the serial monitors shows as a character.

Look at www.asciitable.com and see if the truncated result you are seeing makes sense.

Read and write with the standard EEPROM.h is limited to byte values.

Check out EEPROMWriteAnything at http://playground.arduino.cc/Code/EEPROMWriteAnything

Your variable "time" which picks up millis() should be declared as an unsigned long.

Thanks. I understand now. :)