Go Down

### Topic: How to save an integer in eeprom (Read 10006 times)previous topic - next topic

#### SouthernAtHeart

##### Feb 14, 2011, 12:30 am
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.

#### retrolefty

#1
##### Feb 14, 2011, 12:50 am
Check out this from the Arduino Playground:

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

Lefty

#### SouthernAtHeart

#2
##### Feb 14, 2011, 04:03 am
Thanks!
Quote
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.

#### SouthernAtHeart

#3
##### Feb 14, 2011, 04:55 am
Hmm, I thought something like this code would test out my 3 numbers being saved, but it doesn't compile.
Code: [Select]
`#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.

#### retrolefty

#4
##### Feb 14, 2011, 05:24 am

Maybe just some missing ; ?

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

}

#### SouthernAtHeart

#5
##### Feb 14, 2011, 06:26 am
...I added them, but it's the same error
This is my complete sketch, not just part of it, if that helps debug:

Code: [Select]
`#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);}`

#6
##### Feb 14, 2011, 08:04 am
Try this...

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...

Code: [Select]
`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;}`

6. Remove the two templates from your Sketch.

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

Code: [Select]
`#include <EEPROM.h>#include "writeAnything.h"`

8. Add "configuration." to the front of all "mynumber".

#### SouthernAtHeart

#7
##### Feb 14, 2011, 02:15 pm
Thanks!  Now it compiles.

#### itsthemedication

#8
##### Jul 01, 2011, 06:40 pm
Just to kind of finish up this thread, here is a working code example:

Code: [Select]
`#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.

#### itsthemedication

#9
##### Jul 01, 2011, 09:40 pm
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);