Pages: 1 2 [3] 4   Go Down
Author Topic: Saving arrays in EEPROM using ARDUINOMEGA  (Read 6237 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is stored in address 0 1 and 2 i think.
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 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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is stored in address 0 1 and 2 i think
Think again.
Logged

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

aaaaaaaaaaaaaa tnx!!!!!!

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

TNX ALL!!!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

In case you were wondering:
323 decimal = 0x0143

0x01 + 0x43 + 0xFF (the contents of address 2, because you haven't written anything to it) = 0x143 = 323.
Logged

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another question pls...

If for example I need integer 1511.  I thought that it would need 3 bytes but 2 are needed(serialmonitered it).

Reading addreses 0 and address 1 i get these values
address 0 - 231
address 1 - 5

So my question is, (looking at reply11), 5 is the HIGH byte right?
How is it worked out?
Moving 5 - 101B to shift 8 places - 10100000000 - 1280??
Adding 231 = 1511

Is this right?
tnx
Logged

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is 101B, where did it came from?

But if your high byte is 5, then 5 * 256 =1280. That is correct.
1280 + 231 =1511, so that is true as well.


Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is 101B, where did it came from?
5 in binary.

OP: Start thinking in hex.
1511 is 0x05E7, so two bytes only,  0x05 and 0xe7.
Logged

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

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is 101B, where did it came from?
5 in binary.

OP: Start thinking in hex.
1511 is 0x05E7, so two bytes only,  0x05 and 0xe7.

I'm dummy. That "B" confused me, I though IT WAS hex 101B. I don't know how they should be expressed in these posts, I like to say "bin" or "hex" just to be sure, not combining binary and decimals in the same sentence.
 smiley-wink

Cheers,
Kari

EDIT. By the way, what does the "OP:" stand for? OT= off topic, but OP?
« Last Edit: March 10, 2011, 03:42:27 pm by GaryP » Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Original Poster
Logged

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

Espoo, Finland
Offline Offline
God Member
*****
Karma: 7
Posts: 586
"Oops, try again..."
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Original Poster

Ok, I though it was for me.

This has been very enlighten conversation for me, only thing that bothers me is the short lifeline of the internal eeprom.

If I decide to use external eeprom IC, is there anything that makes life easier or more complicated, comparing to internal eeprom? Is it faster or exactly opposite? Programming issues are more likely the biggest consern, not the speed.

Cheers,
Kari
Logged


The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
only thing that bothers me is the short lifeline of the internal eeprom.

I thought the retention time was of the order of a century - do you plan on leaving your Arduino to your grandchildren?
Logged

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

Pages: 1 2 [3] 4   Go Up
Jump to: