Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #15 on: March 09, 2011, 03:40:00 pm » |
Make it simple - always store and recover "int"s. Read reply #11. Really, there is no problem.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Espoo, Finland
Offline
God Member
Karma: 6
Posts: 581
"Oops, try again..."
|
 |
« Reply #16 on: March 09, 2011, 04:09:08 pm » |
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...
|
|
|
|
« Last Edit: March 09, 2011, 04:12:24 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!!!"
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #17 on: March 09, 2011, 04:15:22 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #18 on: March 09, 2011, 04:42:06 pm » |
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)
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #19 on: March 09, 2011, 04:56:48 pm » |
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???
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #20 on: March 09, 2011, 05:05:53 pm » |
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?)
|
|
|
|
« Last Edit: March 09, 2011, 05:08:33 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #21 on: March 09, 2011, 05:13:36 pm » |
Hehe no it ain't im just stupid lol. Ok so it prints 2 since its 2 bytes....(and NO . mark key aint sticky  ) Yes how can I print the value stored in the EEPROM? tnx
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #22 on: March 09, 2011, 05:18:04 pm » |
Yes how can I print the value stored in the EEPROM? Serial.print(reference); because that's the last value you read back
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #23 on: March 09, 2011, 05:20:35 pm » |
Yes but not like that! I need to know the address and if it has been correctly stored in EEPROM...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #24 on: March 09, 2011, 05:32:36 pm » |
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);
|
|
|
|
« Last Edit: March 09, 2011, 05:38:31 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #25 on: March 09, 2011, 05:36:14 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #26 on: March 09, 2011, 05:41:42 pm » |
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() {}
|
|
|
|
« Last Edit: March 09, 2011, 05:45:51 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 110
Arduino rocks
|
 |
« Reply #27 on: March 09, 2011, 05:48:00 pm » |
SRY AAAAAAA,I didn't upload it (getting late shit)!! Ok so 323  One more question before you kill me please. So 323 is stored in address 0? tnx again
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19020
I don't think you connected the grounds, Dave.
|
 |
« Reply #28 on: March 09, 2011, 05:50:20 pm » |
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.
|
|
|
|
« Last Edit: March 09, 2011, 05:54:46 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #29 on: March 09, 2011, 05:52:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|