How i can write float to EEPROM

Hi guys i'm trying write a float to EEPROM

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

if(result>=31)
{
result=31;

}
}
if(readingpinfaq2==HIGH)
{
result=result-soma;
EEPROM.write(addr, result);
if(result<=18)
{
result=18;

}
}

}

Help me

Tanks

This will make it a lot easier - Arduino Playground - EEPROMWriteAnything.

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 "EEPROMAnything.h" and work. But i will try your code tux, i need understand

Reply later

Tanks !

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.

Had a second look and its a bit more complex. Try this:

#include <EEPROM.h>

float num1 = 63412;
float num2 = 0;

void setup()
{
  Serial.begin(9600);
  delay(2000);
  EEPROM_Write(&num1, 1); // memory position tested as '1'. Memory position
  EEPROM_Read(&num2, 1); // starts at 0 and goes up to max depending on processor.
  Serial.println(num2, DEC);    
}

void loop()
{
}

void EEPROM_Write(float *num, int MemPos)
{
  byte ByteArray[4];
  memcpy(ByteArray, num, 4);
  for(int x = 0; x < 4; x++)
  {
    EEPROM.write((MemPos * 4) + x, ByteArray[x]);
  }  
}

void EEPROM_Read(float *num, int MemPos)
{
  byte ByteArray[4];
  for(int x = 0; x < 4; x++)
  {
    ByteArray[x] = EEPROM.read((MemPos * 4) + x);    
  }
  memcpy(num, ByteArray, 4);
}

Tanks lemming, works fine !

I will this post help another people