South East USA
Offline
God Member
Karma: 2
Posts: 558
|
 |
« on: February 13, 2011, 06:30:55 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15312
Measurement changes behavior
|
 |
« Reply #1 on: February 13, 2011, 06:50:20 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
South East USA
Offline
God Member
Karma: 2
Posts: 558
|
 |
« Reply #2 on: February 13, 2011, 10:03:15 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
South East USA
Offline
God Member
Karma: 2
Posts: 558
|
 |
« Reply #3 on: February 13, 2011, 10:55:35 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15312
Measurement changes behavior
|
 |
« Reply #4 on: February 13, 2011, 11:24:27 pm » |
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);
}
|
|
|
|
|
Logged
|
|
|
|
|
South East USA
Offline
God Member
Karma: 2
Posts: 558
|
 |
« Reply #5 on: February 14, 2011, 12:26:00 am » |
...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);
}
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10137
|
 |
« Reply #6 on: February 14, 2011, 02:04:12 am » |
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; } 6. Remove the two templates from your Sketch. 7. Ensure the following is at the top of your Sketch... #include <EEPROM.h> #include "writeAnything.h"
8. Add "configuration." to the front of all "mynumber".
|
|
|
|
|
Logged
|
|
|
|
|
South East USA
Offline
God Member
Karma: 2
Posts: 558
|
 |
« Reply #7 on: February 14, 2011, 08:15:26 am » |
Thanks! Now it compiles.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #8 on: July 01, 2011, 11:40:56 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #9 on: July 01, 2011, 02:40:10 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);
// 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....
|
|
|
|
|
Logged
|
|
|
|
|
|