writing a big integer to eeprom

I know there have been lots of threads on this, but none of them made it clear to me personally how to write an integer larger than 255 to eeprom. I've seen the write anything library, http://www.arduino.cc/playground/Code/EEPROMWriteAnything, but I'm unclear on the syntax. Could someone take it step by step and show me how to write the integer 300 to eeprom and how to increment the address accordingly( plus one, plus two, I dunno)? thanks!

There are any number of ways to write an integer to EEPROM.

Since an integer is two bytes long, you could use the lowByte() and highByte() functions to extract the two bytes, and write each byte to EEPROM, preferably in consecutive memory locations.

You could use a union to make aa two byte array and an integer refer to the same memory location, then set the integer to the value you want to write, then write the byte array, one element at a time to EEPROM.

okay, I looked up the lowByte and highByte functions, but it doesn't really explain what they do. I would guess that it divides it by 255 or something, but I'm not sure. If you have

val1 = 300;
val2 = lowByte(val1);
val3 = highByte(val1);

what would val1 and val2 be?

val1 = 300;
val2 = lowByte(val1);
val3 = highByte(val1);

300 would be a 16 bit number, in hex it would 0x12c so high byte (val3) would b 0x1 and low byte (val2) would be 0x2c which is 44

1 * 256 = 256

  • 44

300

another thing you can do is use the low level functions included in

#include <avr/eeprom.h>

these are

eeprom_read_word

and

eeprom_write_word

The Arduino library just provides the BYTE read write which is probably the better way to go.

ack. val2 and val3, rather.

ah, great. thanks!

hmm, just to clarify, if I had

int val1 = 7654;
val2 = lowByte(val);
val3 = highByte(val);

7654 in hex is 1de6, so highByte would be 1d and lowByte would be e6, correct?

correct