Saving arrays in EEPROM using ARDUINOMEGA

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

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)

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

This is my code:

#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???

Yes it works perfectly.
"reference" is two bytes long, so 2 is printed

Did you mean to print the value of "reference" you read back from the EEPROM?

(Is your question mark key a bit sticky?)

Hehe no it ain't im just stupid lol.
Ok so it prints 2 since its 2 bytes....(and NO . mark key aint sticky :P)

Yes how can I print the value stored in the EEPROM?
tnx

Yes how can I print the value stored in the EEPROM?

Serial.print(reference);

because that's the last value you read back

Yes but not like that!
I need to know the address and if it has been correctly stored in EEPROM...

I need to know the address and if it has been correctly stored in EEPROM

Only YOU know the address, but if you want to see what was read back, try

EEPROM_writeAnything(0, reference);
EEPROM_readAnything(0,serial);
Serial.print(serial);

2...
But how can it be that I am storing the reference # inside address 0 only?(I dont think it is...)
since if i write
serial = eeprom.read(0)
323 is not displayed

2...

Now your full-stop (period) key is sticking.
Turn your keyboard over and give it a shake.

Let me post the sketch as I have it at the moment, just so there is no further misunderstanding:

#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 serial;
 
  EEPROM_writeAnything(0, reference);
  EEPROM_readAnything(0,serial);
       
  Serial.print(serial);
}
void loop()
{}

SRY AAAAAAA,I didn't upload it (getting late shit)!!
Ok so 323 :smiley:
One more question before you kill me please.
So 323 is stored in address 0?

tnx again

So 323 is stored in address 0?

Don't be afraid, I won't kill you!

No, 323 cannot be stored in address zero because 323 is too big to be stored in a single byte, so it is stored in address 0 and address 1.

No, 323 cannot be stored in address zero because 323 is too big to be stored in a single byte, so it is stored in address 0 and address 1.

Which is why EEPROM_readAnything returned 2. That is the next available address to write to/read from.

It is stored in address 0 1 and 2 i think.

#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 serial;
 
  EEPROM_writeAnything(0, reference);
  //EEPROM_readAnything(0,serial);
  serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
       
  Serial.print(serial);
}
void loop()
{}

serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
Removing read(2) will not give 323

It is stored in address 0 1 and 2 i think

Think again.

serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
This 'prints' 323

serial = EEPROM.read(0) + EEPROM.read(1)
This 'prints' 68

why then??
(And hehe i am on a laptop so shaking my keyboard wont make it that better :p)

tnnx

The value is not stored digit by digit in consecutive addressed. I think you need to go back and look at reply #11 (not to sound like a broken record or anything).

"You provide the first EEPROM address to be written, and the functions return how many bytes were transferred."
So if in this code it returns 2, meaning 2 bytes were transferred right?

But still I cannot understand the fact that using the code; EEPROM.read(0) + EEPROM.read(1) it 'prints' 68.

I'm sorry if im being such a bi*** but I really need to understand this

tnx again

The function returns the high byte and the low byte on the two calls. The integer is constructed by shifting the high byte 8 places to the left and adding the low byte, as was shown in reply #11. It is NOT constructed by simply adding the two bytes.

The values that you are getting are 1 and 67. Shifting the 1 8 places is equivalent to multiplying by 256. So, 256 + 67 = ? That's right. 323.

aaaaaaaaaaaaaa tnx!!!!!!

now for tomorrow to continue looking in this....

TNX ALL!!!