Trying to write/read double values to eeprom[SOLVED]

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);
}

I'm simply trying to write double values to eeprom if anyone has a better idea. Any help is greatly appreciated, thanks!

int writeValue - 1.25;

I don't think so.

EEPROM

write()
Description

Write a byte to the EEPROM.

http://arduino.cc/en/Reference/EEPROMWrite

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

writing a value with decimal point isn't working for me yet.

A single byte doesn't have a decimal point.

Maybe someone should float you an answer....

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.

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)

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

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.

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.

This has been covered before in:

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

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. :slight_smile: