Go Down

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

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.

Go Up