Writing and reading float using Arduino EEPROM

This post is just for a personal reminder after reading discussions from elsewhere about storing and reading float values to/from EEPROM with Arduino.

Scenario


Writing negative float to EEPROM can be tricky, since EEPROM only recognises up to 8-bit values (see Tronixstuff  explanation), therefore it requires an additional algorithm to make it able to store negative and float.  the scheme is to use four bits of the ATmega328's EEPROM to store numerical parts of a float value. The function involved is union.

//union  value
typedef union{
  float flt;
  byte array[4];
} FloatConverter;

The code


You may grab the code here


Comments

Popular posts from this blog

Merakit Destilator Bioetanol Sederhana (1)

DHT11 incubator with I2C LCD (part 1)

LM35 Incubator with LCD 16x2 on Arduino