Of course EEPROM can only write int...So i really dont understand how i can write a float number ?
I read many topics here, but i still dont understand
Here a part of my code
float result;
float soma=0.5;
if(state==LOW) // Menu temperature set
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T.A.");
lcd.setCursor(0, 1);
lcd.print(result);
delay(1000);
readingpinaq = digitalRead(pinup);
readingpinfaq2 = digitalRead(pindown);
if(readingpinaq==HIGH)
{
result=result+soma;
EEPROM.write(addr, result); // at this part i want write this float result
Of course EEPROM can only write int...So i really dont understand how i can write a float number ?
No, EEPROM stores bytes. The meaning of those bytes is program-dependent.
Think about an integer variable or a float variable as a sequence of sizeof(variable) bytes. Each single byte can be transferred from RAM to EEPROM or vice-versa. In fact, you write or read one byte at a time to/from EEPROM.
Let me try with a pseudo-code example:
EEPROM.write(addr, result);
becomes:
ramAddr = &result;
eepromAddr = addr;
for i = 0 to sizeof(result) do:
b = value of byte at ramAddr + i
eeprom.writebyte(eepromAddr + i, b)
done
I use the following to write setup settings to EEPROM:
int NoElements = 3;
unsigned int setting_table[3][3] = {
{ 20, 20, 50},
{ 10, 0, 600},
{ 150, 20, 255},
};
..................
................
.......................
int ReadSetup()
{
for (int i = 0; i < NoElements; i++)
{
byte h = EEPROM.read(2 * i);
byte l = EEPROM.read((2 * i) + 1);
setting_table[i][0] = word(h, l);
}
return 0;
}
int WriteSetup()
{
for (int i = 0; i < NoElements; i++)
{
byte h = highByte(setting_table[i][0]);
byte l = lowByte(setting_table[i][0]);
EEPROM.write(2 * i, h);
EEPROM.write((2 * i) + 1, l);
}
return 0;
}
Haven't got time to adapt to your sketch but come back if you're stuck and I'll have a look at it later.
The EEPROM only accepts bytes while an integer is two bytes. Double or floats use four bytes, therefore use a "4" instead of the "2"s above and use something other than highByte/lowByte to break up the double into its four constituent bytes.