Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« on: February 01, 2013, 02:44:57 am » |
Hi, I'm trying to use eepromwriteanything. for some reason this simple sketch keeps printing 1.00 to serial? Any ideas? #include <EEPROM.h>
#include <EEPROM_writeAnything.h>
int address = 0; int writeValue = 1.25; double readValue;
void setup() { Serial.begin(9600); }
void loop()
{ EEPROM_writeAnything(address, writeValue); readValue = EEPROM.read(address);
Serial.println(readValue); address = address + 1; delay(500); }
|
|
|
|
« Last Edit: February 01, 2013, 03:06:07 am by noobdude »
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« Reply #1 on: February 01, 2013, 02:46:30 am » |
I'm simply trying to write double values to eeprom if anyone has a better idea. Any help is greatly appreciated, thanks!
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #2 on: February 01, 2013, 02:49:32 am » |
int writeValue - 1.25;
I don't think so.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: February 01, 2013, 02:50:05 am » |
EEPROM
write() Description
Write a byte to the EEPROM. http://arduino.cc/en/Reference/EEPROMWrite
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« Reply #4 on: February 01, 2013, 02:56:49 am » |
@Bob That's hilarious lol. This was a quick sketch I wrote up. Should have looked it over a little better lol. Too bad even with declaring the write variable as double its still not working just like my original sketch.
@AWOL reading and writing is very easy so far although, writing a value with decimal point isn't working for me yet.
I tried the eepromex library to no avail either....
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: February 01, 2013, 02:59:15 am » |
writing a value with decimal point isn't working for me yet.
A single byte doesn't have a decimal point.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #6 on: February 01, 2013, 03:03:32 am » |
Maybe someone should float you an answer....
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« Reply #7 on: February 01, 2013, 03:04:21 am » |
Float an answer lol yeah. Sometimes I feel like I'm the first noob to try to do something and searching comes up with nothing. I'm sure its just me. @AWOL Exactly. I'm not exactly sure how this works but it does using the eepromex library. #include <EEPROMEx.h>
int address = 0; double input = 1.25; double output = 0;
void setup() { Serial.begin(9600);
}
void loop() { EEPROM.writeDouble(address, input); output = EEPROM.readDouble(address); Serial.print(address); Serial.print("\t"); Serial.print(output); Serial.println(); address = address + 1; delay(500); }
Any ideas? This is 100% working code though and I'll use this. I'm not sure why my first sketch didn't work, as I did it identical or I thought anyways.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: February 01, 2013, 03:05:47 am » |
I'm not sure why my first sketch didn't work, Because reading and writing a single byte is not the same as reading and writing a float (which is the same size as a double on the Arduino)
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #9 on: February 01, 2013, 03:07:34 am » |
"value: the value to write, from 0 to 255 (byte)" and "A byte stores an 8-bit unsigned number, from 0 to 255."
So, no decimals allowed. Looks like you need to split your number into 2 parts, the whole part and decimal part. Multiply the decimal part by 10, store the two resulting integers and do the reverse when you read them back. There may well be a library to do this that hides the mechanics of how it works. As I have never used the Arduino EEPROM I don't know if there is a better way.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« Reply #10 on: February 01, 2013, 03:09:26 am » |
There I go again.. I know why eeprom.h didn't work. I meant my original code that wasn't posted here. I 'thought' I did the same thing in my code as in the 100% working sketch. Just glad its working now. I get to finish up tomorrow. Too tired I think that's obvious haha!
Thanks again for all your input!!!!!!!
If you guys try the code it works. Now, I'm not 100% on 'how' it works but it does. When I go to add this into my real sketch I'll figure it out though so I know where to save info etc.
|
|
|
|
« Last Edit: February 01, 2013, 03:11:05 am by noobdude »
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #11 on: February 01, 2013, 03:18:05 am » |
Maybe that library uses a template. In which case your 16-bit int writeValue was == 1 and that's what you got back out.
Also Arduino double is the same as Arduino float, both 32 bits. You're better off and way faster using fixed-point with long (32-bit) or long long (64-bit) variables.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Australia
Offline
Sr. Member
Karma: 8
Posts: 326
Arduino rocks
|
 |
« Reply #12 on: February 01, 2013, 05:04:42 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 68
|
 |
« Reply #13 on: February 06, 2013, 02:01:48 am » |
Thanks for reply. I ended up just simply multiplying the double value by 100 then writing to eeprom. After that I used a variable to / by 100 into the sketch. Works pefectly. 
|
|
|
|
|
Logged
|
|
|
|
|
|