Go Down

Topic: Trying to write/read double values to eeprom[SOLVED] (Read 1 time) previous topic - next topic

noobdude

Feb 01, 2013, 08:44 am Last Edit: Feb 01, 2013, 09:06 am by noobdude Reason: 1
Hi, I'm trying to use eepromwriteanything.  for some reason this simple sketch keeps printing 1.00 to serial?  Any ideas?

Code: [Select]
#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);
}



noobdude

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

UKHeliBob

[font=Verdana]int [/font]writeValue - 1.25;

I don't think so.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

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

noobdude

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

AWOL

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

A single byte doesn't have a decimal point.
"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.

GoForSmoke

Maybe someone should float you an answer....

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

noobdude

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: [Select]
#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.

AWOL

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

UKHeliBob

"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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

noobdude

#10
Feb 01, 2013, 09:09 am Last Edit: Feb 01, 2013, 09:11 am by noobdude Reason: 1
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.

GoForSmoke

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.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

lemming


noobdude

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

Go Up