How to save an integer in eeprom

I need to store 5 integers in eeprom. I kinda follow the example sketches given on eeprom write and right, but they are for one byte.
Could someone please give me an example sketch of how to write and then read just 1 integer in eeprom. The integer could be either 2,3, or 4 digits, depending on user selected input.
If I can see how to do this for 1 interger, then I can figure out how to save/load all 5 of my integers, maybe starting each one at different addresses in eeprom like 10, 20, 30, 40, & 50.
Thanks.

Check out this from the Arduino Playground:

http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Lefty

Thanks!

It will be proposed for built-in support in a future release like 0014.

...I'd vote for it! But we're already up to release 0022, so it must not be popular.

Hmm, I thought something like this code would test out my 3 numbers being saved, but it doesn't compile.

#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;
}


struct config_t
{
    int mynumber1;
    int mynumber2;
    int mynumber3;
} configuration;

void setup()
{
    EEPROM_readAnything(0, configuration);
    Serial.begin(9600);       // use the serial port
    Serial.println(mynumber1, DEC);
    Serial.println(mynumber2, DEC);
    Serial.println(mynumber3, DEC);
    // ...
}
void loop()
{
    // let the user adjust their settings
    // I'll put values here to simulate user selected options
    mynumber1 = 1035
    mynumber2 = 34
    mynumber3 = 845

    // if they push the "Save" button, save their configuration
    if (digitalRead(13) == HIGH)
        EEPROM_writeAnything(0, configuration);

}

error: expected "," or "..." before "&" token
I don't know where the error is.

Maybe just some missing ; ?

void loop()
{
// let the user adjust their settings
// I'll put values here to simulate user selected options
mynumber1 = 1035;
mynumber2 = 34;
mynumber3 = 845;
// if they push the "Save" button, save their configuration
if (digitalRead(13) == HIGH)
EEPROM_writeAnything(0, configuration);

}

[/quote]

...I added them, but it's the same error
This is my complete sketch, not just part of it, if that helps debug:

#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;
}


struct config_t
{
    int mynumber1;
    int mynumber2;
    int mynumber3;
} configuration;

void setup()
{
    EEPROM_readAnything(0, configuration);
    Serial.begin(9600);       // use the serial port
    Serial.println(mynumber1, DEC);
    Serial.println(mynumber2, DEC);
    Serial.println(mynumber3, DEC);
    // ...
}
void loop()
{
    // let the user adjust their settings
    // I'll put values here to simulate user selected options
    mynumber1 = 1035;
    mynumber2 = 34;
    mynumber3 = 845;

    // if they push the "Save" button, save their configuration
    if (digitalRead(13) == HIGH)
        EEPROM_writeAnything(0, configuration);

}

Try this...

  1. Load your Sketch.

  2. In the upper-right corner of the IDE is a white outlined button with an arrow pointing right. Click it.

  3. Click "New Tab".

  4. Enter "writeAnything.h" and click OK.

  5. In the "writeAnything.h" tab put the following code...

typedef unsigned char byte;

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;
}
  1. Remove the two templates from your Sketch.

  2. Ensure the following is at the top of your Sketch...

#include <EEPROM.h>
#include "writeAnything.h"
  1. Add "configuration." to the front of all "mynumber".

Thanks! Now it compiles.

Just to kind of finish up this thread, here is a working code example:

#include <EEPROM.h>
#include "writeAnything.h"

struct config_t
{
    int myInteger;
} int2IEeprom;

byte tracker=0;

void setup()
{
  Serial.begin(9600);
  
  int2IEeprom.myInteger=21345;
  tracker=EEPROM_writeAnything(0,int2IEeprom);
  Serial.println(tracker,DEC);
  Serial.println(int2IEeprom.myInteger,DEC);
  
  int2IEeprom.myInteger=0;
  Serial.println(int2IEeprom.myInteger,DEC);
  
  EEPROM_readAnything(0,int2IEeprom);
  Serial.println(int2IEeprom.myInteger,DEC);  
}

void loop()
{
  
}

I added this because the structure, as used, is not using the "taught" method, where you would initialize a variable with the structure name. However, this method has advantages. I also included a "tracker" byte that show the number of bytes being used in eeprom for the call being made. If you are storing lots of data, you'll definitely want to maintain a cumulative "track" of your current unused address space.

Oh, and the really easy way to just do an integer to eeprom is:

// writing to address 0 and 1:

int yourInteger=21345;
EEPROM.write(0,highByte(yourInteger);
EEPROM.write(1,lowByte(yourInteger);

// Reading from address 0 and 1:

byte high = EEPROM.read(0);
byte low = EEPROM.read(1);
int myInteger=word(high,low);
Serial.println(myInteger,DEC);

Anything does anything, but this is the quick and dirty for integers....

Even more brutal and dirty:

#include <EEPROM.h>
/* some code */

int ValueStoredInEEPROM = word(EEPROM.read(0), EEPROM.read(1));

void setup () {
/* some code */
}

void loop () {
/* some code /
EEPROM.write(0, highByte(ValueStoredInEEPROM ));
EEPROM.write(1, lowByte(ValueStoredInEEPROM ));
/
some code */
}

It's work for me.

Hmm, seven-and-a-half years later (actually eight years minus a few weeks).

Nowadays one simply uses EEPROM.put and EEPROM.get ; no hacks needed.