Writing and Reading unsigned longs to EEPROM

Hi, I'm hoping someone can help me with an example of a short piece of code that will write a decimal value, say 33509 to EEPROM and then read it back from EPROM and display the same value.

I can write the high and low bytes to EEPROM however when I read them back I get a different number.

Thanks... :confused:

Use the get and put methods detailed in the EEPROM library reference.

It would help to see the source code or at least its snippet.

1. Your given decimal number is: 33509

2. When the number of Step-1 is saved in computer memory, it gets stored in natural binary which is: 1000 0010 1110 0101 = 0x82E5 in hex base. It is a 16-bit unsigned number. Why are you saying about unsigned long type which is a 32-bit number? An unsigned long int type number has this range: 0 to 4294967295 = 0x00000000 - 0xFFFFFFFF.

3. When we cite a decimal number, it is important that we keep a feeling on the size of the corresponding binary (hex = compact form of binary) number using Calculator or Serial.print() function.

Examples:

unsigned int x = 33509;  //memory holds 82E5 through automatic conversion
EEPROM.put(0x0010, x);   //saving data into two consecutive memory locations starting at 0x0010
unsigned int y;             
EEPROM.get(0x0010, y);   //reading data back from memory locations and save into variable y
Serial.print(y);                  //Serial Monitor shows: 33509

Thanks, I'll give it a try.

IMHO the get/put methods are just a bit of pain.

The most easy way to use eeprom is to define a structure:

//============= Eeprom Persistance ==============
struct settings_t
{
  byte            reportType;
  byte            displayMode;
  unsigned int    adc94;
  unsigned int    adc47;
  int             corrdB;
  boolean         dualChannel;
} settings;

Then whenever you need a value, you use it with e.g.

settings.reportType

Just save all data in the eeprom with:

eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));

recall it with:

eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));

That is by far the easiest way to deal with eeproms.
You don't have to worry with sizes, with last digit, highest digit, you can use whatever you want and mix: texts, integers, floats ad libitum.

‚Ķor you could save and retrieve your struct using the EEPROM library‚Äôs put and get methods, then you don‚Äôt have to worry about sizeof operations or crazy ‚Äúvoid*‚ÄĚ casts.

RIN67630:
IMHO the get/put methods are just a bit of pain.

Do you have an explanation for that opinion?

sterretje:
Do you have an explanation for that opinion?

Let us cite KoolDude: "I can write the high and low bytes to EEPROM however when I read them back I get a different number."

If you don't want to scratch your head to figure out how each variable type is stored in memory each time you need one, and begin to do the math to find out where it is stored in eeprom, forget all that stuff, use a structure and enjoy life!

Edit: erratum: I wrote "get and put" methods and meant "read and write".

So, rather than the read and write methods, s/he should use the get and put methods.
No casts, no sizeof.