Pages: [1]   Go Down
Author Topic: How can i write/read an integer to eeprom?  (Read 983 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int temp;

how can i write to eeprom the content of temp? what if it has more than 1 digit? for example "123"
does the code need to split to 1,2,3 and write to 3 adresses?
Logged

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

123 is less than 255 which is the largest value you can store in a byte.
However "123" is a string and takes up four bytes.
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
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah mistake i mean 123 not "123"

but what about 654?
and what is the syntax to do that?
do i need to split it and write 6,5,4 in 3 addresses? and how can i join them?(on reading)
« Last Edit: May 15, 2010, 01:59:46 pm by acmilangr » Logged

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

Code:
int x = 645;
byte hix = highByte (x);
byte lox = lowByte (x);
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
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int x = 645;
byte hix = highByte (x);
byte lox = lowByte (x);

thanks for that, a little explain? :-? what value will have the hix and what the lox on this example?


mmm is the split of 645 in 2 bytes?

searching on the internet i saw this
elocity = -200 = hex FF38 = [hex FF] [hex 38] = [255] [56]
Radius = 500 = hex 01F4 = [hex 01] [hex F4] = [1] [244]

so i think i undestand now..

and another question. do i need to check a demical if it is >than 256 to use high/low fuction before i save it to eeprom? and how do i know when i read from eeprom if the value have 1 or 2 addreesses? if i read always 2 adresses then if the number i saved was less than 256 i'll get wrong result


and how i merge them? smiley-wink
« Last Edit: May 15, 2010, 02:35:57 pm by acmilangr » Logged

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

(hix * 256) + lox
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.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16564
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and another question. do i need to check a demical if it is >than 256 to use high/low fuction before i save it to eeprom?

Integers have no decimal point.

and how do i know when i read from eeprom if the value have 1 or 2 addresses?
 
eeproms only directly write or read to byte addresses, so you always have to write or read two bytes to save or retrieve a integer number no matter what it's value is.

if i read always 2 addresses then if the number i saved was less than 256 i'll get wrong result

No you will get correct results (even if <256) if you use AWOL's formula,  x= (hix * 256) + lox;

Lefty
« Last Edit: May 15, 2010, 02:59:39 pm by retrolefty » Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

search for eepromwriteanything.

Mowcius
Logged

Pages: [1]   Go Up
Jump to: