Floating point value into EEPROM

hello All,
i want to save multidimensional array (decimal value) into EEprom. it is working for integer but sometime i need to save decimal value too.
can somebody help me in this regards??
thanks in advance.

#include <EEPROM.h>
#include <SPI.h>
const int rows = 2;
const int columns = 3;
int array1[ rows ][ columns ] = { { 55.5, 2, 3 }, { 4, 5, 6 } };
int addr;
void setup () {
Serial.begin(9600);

}
void loop () {
Serial.println ("Values in array1 by row are: ") ;

printArray(array1) ;

}

void printArray( const int a[][ columns ] ) {

for ( int i = 0; i < 1; ++i ) {

  for ( int j = 0; j < 1; ++j )
  Serial.println (a[ 0 ][ j] );
  addr= a[0][columns];

  EEPROM.update(addr, a[0][columns]);
  Serial.println (addr);

}
}

An obvious solution to this is to realize that a float value in Arduino is basically just a memory area of 4 bytes, which you can store one by one in EEPROM. When retrieving them, you do the opposite.

So effectively you would have a routine that reads each individual byte from the float value (regardless of what the value of the float looks like), and a routine that reads them back from EEPROM for retrieval.

is it work if i use double command. double command has 8 bytes.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

1 Like

Where do you think that "55.5" floating point number is going ?
Spoiler: Into thin air. Only the whole number of "55" is to heavy to fly away and that get stored in the integer.

Is there any reason not to use EEPROM.put() and EEPROM.get() to do what you want ?

#include <EEPROM.h>

float putArray[3][3] =
{
  {1, 2, 3},
  {1001, 1002, 1003},
  {2001, 2002, 2003}
};
float getArray[3][3];

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  EEPROM.put(0, putArray);
  EEPROM.get(0, getArray);
  for (int row = 0; row < 3; row++)
  {
    for (int col = 0; col < 3; col++)
    {
      Serial.print(getArray[row][col],2);
      Serial.print(",");
    }
    Serial.println();
  }
}

void loop()
{
}
2 Likes

What processor are you using? In Arduino, double is the same as float, 4 bytes.

i am using Arduino Due.

I am sorry, i am beginner in programming and trying hard to learn it.
It is just an example. I am going to use array to store 3 different sensor values and every column is specify for one sensor value.
could you please help me to figure it out how can i update Row values for Sensor 1.
i am comparing the two output of sensor on a different time (t1= 34m3, t2= 55.3m3) and if t1 is greater or equals to t2, t1 value shall go to a[R1][C1] and t2 value shall store to a[R2][C1].

You have three sensors and assume that these are S1, S2, and S3. What types of sensors are they? Does each sensor produce one output? What is the range of the output of S1 in floating point format (For example)?

Note that:
In Arduino UNO, NANO, MEGA and DUE, float allocates 4-bte memory space to hold floating point number of binary32 standard.

In Arduino DUE, double allocates 8-bte memory space to hold floating point number of binary64 standard.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.