Saving & retrieving Floats in EEPROM

I want to periodically store a Float variable in EEPROM to get me through reboots, but the EEPROM Reads & Writes only apply to bytes. I've tried playing with various pointers but made no progress. My CCS-C for the PIC has pre-processor directives that let me define a 4-byte array and a Float to share the same address. That would seem to offer a solution. i.e., I could just write the byte array and it would effectively save the Float. Likewise for retrieval. But I don't find a way in Arduino-speak to set this same equuivalence. Would appreciate any references to help.
TIA,
Don

Have you tried EEPROM Write Anything library?

What you are describing is called a union in C/C++. You can also find useful functions for use of the eeprom in the avr-libc docs, avr/eeprom.h. avr-libc: <avr/eeprom.h>: EEPROM handling

Thanx for both suggestions. Got the UNO last birthday, but this is my first foray into it.

I think you can find EEPROM Write Anything in the Learning:Playground area.

Making good progress!
#included the avr/eeprom.h library.
The eeprom_read_block compiles fine. But when I put in the eeprom_update_block statement I get an error
'eeprom_update_block' was not declared in this scope.
If I merely change that statement to an eeprom_write_block, it compiles OK. This leads me to think that the update call isn't there (or is spelled differently).
Looks like I'm missing something simple.
TIA
Don

Looks like I'm missing something simple.

So are we - your code.

  #include <avr/eeprom.h>
  
  int ma_value ; // 4-20 = 200-1000 = 0-240kw
  int seconds ;
  float kwhr[4] ;
  float kwh ; 
  
  
  void setup()
  {
    int i ;
    pinMode(13, OUTPUT) ;
    delay(1000) ;
    Serial.begin(9600) ;
    Serial.write(22) ; //  cursor off
    eeprom_read_block ( (void*)kwhr, (const void*) 0, 4 ) ;
  }
  
  void erase_line( int x )  //  x is line 0 or 1
  {
    int i ;
    if( x==0 )
    {
      Serial.write(128) ;
      for( i=0; i<16; i++ ) Serial.write(32) ;
    }
    else
    {
      Serial.write(148) ;
      for( i=0; i<16; i++ ) Serial.write(32) ;
    }
  } 
  
  void cursorToLine( int x )
  {
    if( x==0 )  Serial.write(128) ;
    else  Serial.write(148) ;
  } 
    
  
  void loop() 
  {
    float kilowatt ;
    digitalWrite(13, HIGH) ; // LED On
    ma_value = analogRead(0) ;
    
    erase_line(1) ;
    cursorToLine(1) ;
    Serial.print( ma_value ) ;
    
    kilowatt = map( ma_value, 0, 1023, 000, 24000 ) ;
    erase_line(0) ;
    cursorToLine(0) ;
    Serial.print( kilowatt ) ; 
    kwh += kilowatt/1000 ;
    Serial.write(137) ; // line 0 pos 9
    Serial.print( kwh ) ;
    delay(900) ;
    
    digitalWrite(13,LOW) ; // LED Off
    seconds++ ;
    if( seconds == 60 )
    {
      seconds = 0 ;
      eeprom_update_block( (const void*)kwhr, (void*) 0, 4 ) ;
    }
    delay(100);
  }

This gives me the compile error:
'eeprom_update_block' was not declared in this scope
for the update block call.
If I change 'update' to 'write', it compiles OK.

pomplun:
I want to periodically store a Float variable in EEPROM to get me through reboots, but the EEPROM Reads & Writes only apply to bytes.

Grab the attached ZIP file. It replaces the stock EEPROM library files (the .cpp and .h files).

It gives you:

EEPROM.read (synonym for readByte)
EEPROM.readByte
EEPROM.readWord
EEPROM.read.DWord
EEPROM.readQWord
EEPROM readFloat
EEPROM.readDouble

... and the cooresponding EEPROM.write functions.

Of course, you need to know how large each data type is. If, for example, you do a "EEPROM.writeDWord (0, value)", then the next one has to be "EEPROM.writeDWord (4, value)" because the first write used locations 0, 1, 2 and 3.

(and to those who just HAVE to bump their post count by telling me that "float" and "double" are the same thing in Arduino, yeah I already know that. But maybe some day the AVR toolchain will have real double support and then this library will be ready).

EEPROM.zip (1.58 KB)

Thanx; always good to find it isn't just you!
Since this is for a totalizing electric meter (which is always increasing) I don't need to do Updates since data will Always be different. But thanx anyway.
Don

Krupski:

pomplun:
I want to periodically store a Float variable in EEPROM to get me through reboots, but the EEPROM Reads & Writes only apply to bytes.

Grab the attached ZIP file. It replaces the stock EEPROM library files (the .cpp and .h files).

It gives you:

EEPROM.read (synonym for readByte)
EEPROM.readByte
EEPROM.readWord
EEPROM.read.DWord
EEPROM.readQWord
EEPROM readFloat
EEPROM.readDouble

... and the cooresponding EEPROM.write functions.

Of course, you need to know how large each data type is. If, for example, you do a "EEPROM.writeDWord (0, value)", then the next one has to be "EEPROM.writeDWord (4, value)" because the first write used locations 0, 1, 2 and 3.

(and to those who just HAVE to bump their post count by telling me that "float" and "double" are the same thing in Arduino, yeah I already know that. But maybe some day the AVR toolchain will have real double support and then this library will be ready).

Hello Krupsi,
do You have that lib also for i2c eeprom???

regards
Joerg