Pages: [1]   Go Down
Author Topic: Trying to write/read double values to eeprom[SOLVED]  (Read 851 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm trying to use eepromwriteanything.  for some reason this simple sketch keeps printing 1.00 to serial?  Any ideas?

Code:
#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 Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 90
Posts: 3516
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int writeValue - 1.25;

I don't think so.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe someone should float you an answer....

Logged

Examples can be found in your IDE.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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 Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3516
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 in your IDE.

Australia
Offline Offline
Sr. Member
****
Karma: 10
Posts: 397
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This has been covered before in:

http://arduino.cc/forum/index.php/topic,122270.0.html
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: