Go Down

Topic: Saving arrays in EEPROM using ARDUINOMEGA (Read 6 times) previous topic - next topic

AWOL

Make it simple - always store and recover "int"s.
Read reply #11.
Really, there is no problem.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GaryP

#16
Mar 09, 2011, 10:09 pm Last Edit: Mar 09, 2011, 10:12 pm by GaryP Reason: 1
I don't know about the application and description of the needs, but simple way to get high and low byte would be like this.

If your integer is 1234, just do 1234/256=4
4 is the value of your highbyte

Then next you take (4*256) out from your original int; 1234 - (4*256) =210
210 is your lower byte.

Then, when you need to know the address your about to write these values.

If the pointer in the array is, lets say 51, so you have value 1234 in YourArray[51], you write it to eeprom;
EEPROM.write( (2*51) , lowByte );
EEPROM.write( ( (2*51) + 1) , highByte );

I'm a bit tired, so there's probably errors in my text, please, don't hesitate to say that was bull...it.

Cheers,
Kari

EDIT. I have never used eeprom library, that maybe wrong syntax...
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Daniel Formosa

Yes i read it.
So do I put the writeanyting and readanyting as functions in my program?
Can I save them in a library of some sort?

tnx

AWOL

Quote
Can I save them in a library of some sort?

You could put them in a simple header file - they are simple templates.
As long as you know how big your objects are, you can store and read back any size, up to the maximum size of the EEPROM on your processsor.
I'd probably recommend describing a single struct to store all the values you need to save, so a single call will save/restore all your values, though I'd probably rewrite "writeAnything" to only write a byte if it had changed (read it first to compare)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

The "readAnything", is not working prop...

This is my code:
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;
}
void setup()
{
  Serial.begin(9600);
  int reference = 323;
  int gas = 1234;
  int serial;
  int serial1;
 
 
    EEPROM_writeAnything(0, reference);
    serial = EEPROM_readAnything(0,reference);
   
   
   Serial.print(serial);
   
   
}
void loop()
{
   
}




2 is displayed now.

Does it work properly???


Go Up