EEPROM.write question

I need to store some data for retrieval after a re-boot. The EEPROM will only store a byte in each location. So if my number is less than 255 (0xFF) I am ok, but how do I store a number larger than that? Say 300, and retrieve it later?

Do I need to do something like this (pseudo code)

i = 300;
if(i>0xFF)
{
b = i-0xFF;
i = 0xFF;
EEPROM.write(0,i);
EEPROM.write(1,b);
}

Thanks

I'd probably go with:

int i = 24601;

uint8_t il = i & 0xFF;
uint8_t ih = i>>8;

EEPROM.write(0, il);
EEPROM.write(1, ih);

Ah, I think got it, but not sure,

Then to put it back together,

il = EEPROM.read(0);
ih = EEPROM.read(1);

ih = ih<<8;
i = ih | il;

something like that?

And where can I find info on Unit8_t?

Probably more like:

byte il = EEPROM.read(0);
byte ih = EEPROM.read(1);

int i = ih; // grab the high byte
i <<= 8; // shift the high byte left
i += il; // add in the low byte

There is probably a way to do it in fewer statements but this shows the steps required.

You just have to decide up front how many EEPROM locations you need for each value you want to store and whether you are storing low byte to high byte or the other way around.

Jassper, a uint8_t is an unsigned byte (8 bits).

Your reconstruction is almost right, but if you do

 ih = ih << 8;

you'll get 0 every time. Better:

i = ((int)ih << 8) | il;

Cool, I think I got enough to play with now.

Thanks to all,

What do you know, it works!! thanks.

Test script

// Test EEPROM Write/Read
#include <EEPROM.h>

int i = 476;
int il;
int ih;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
  // Delay startup
  delay(2000);

  if(i == 476)
      {
      Serial.println("begin EEPROM Write");
      Serial.print("i equals ");
      Serial.println(i,DEC);
      Serial.println("Writeing EEPROM...");
      il = i&0xFF;
      ih = i>>8;
      EEPROM.write(0,il);
      Serial.print("Data Low = ");
      Serial.print(il,DEC);
      Serial.print(" .. ");
      Serial.println(il,HEX);
      EEPROM.write(1,ih);
      Serial.print("Data High = ");
      Serial.print(ih,DEC);
      Serial.print(" .. ");
      Serial.println(ih,HEX);
      Serial.println("Clearing i, il, ih");
      i = 5; il = 0; ih = 0;
      Serial.print("i = ");
      Serial.print(i,DEC);
      Serial.print("| il = ");
      Serial.print(il,DEC);
      Serial.print("| ih = ");
      Serial.println(ih,DEC);
      delay(1000);
      }
   else if(i == 5)
     {
     delay(1000);
     Serial.println("Attempting to re-construct i");
     Serial.print("i = ");
     Serial.print(i,DEC);
     Serial.print("| il = ");
     Serial.print(il,DEC);
     Serial.print("| ih = ");
     Serial.println(ih,DEC);
     Serial.println("Reading Data Low");
     il = EEPROM.read(0);
     Serial.print("Data Low = ");
     Serial.print(il,DEC);
     Serial.print(" .. ");
     Serial.println(il,HEX);
     ih = EEPROM.read(1);
     Serial.print("Data High = ");
     Serial.print(ih,DEC);
     Serial.print(" .. ");
     Serial.println(ih,HEX);
     Serial.println("Rebuilding ....");
     i = ((int)ih << 8) | il;
     Serial.print("i = ");
     Serial.println(i,DEC);
     Serial.println("DONE");
     i = 0;
     }
}

Results

begin EEPROM Write
i equals 476
Writeing EEPROM...
Data Low = 220 .. DC
Data High = 1 .. 1
Clearing i, il, ih
i = 5| il = 0| ih = 0
Attempting to re-construct i
i = 5| il = 0| ih = 0
Reading Data Low
Data Low = 220 .. DC
Data High = 1 .. 1
Rebuilding ....
i = 476
DONE

Excellent, glad to hear it.