Write analog value in SD

Hello to all.

I have a problem with a program that I am doing. I am read an analog value and I want to writte this value in a file in the SD. The problem is that this value is write as int ( 283 for example) and I want see this value in float (1.38).
I am using this code but I can not storage this value in float.

File myfile = SD.open(nomfichero,FILE_WRITE);
if (myfile){

String dataString = "";
for (int analogPin = 0; analogPin < 1; analogPin ++){
float sensor = analogRead(analogPin)*(5,0 / 1023,0);
char sensor2 = sensor;
dataString += String(sensor2);
if (analogPin < 2){
dataString += ",";
}
}

myfile.println(sensor);
myfile.close();

I am very thank fully if somebody could help me.

Thanks !!!

        float sensor = analogRead(analogPin)*(5,0 / 1023,0);                            
        char sensor2 = sensor;

Do you think a float is going to fit in a char?

Ditch the String class. It is not needed for writing to the SD card.

Arduino float is 32 bits, 8 bytes in memory. You can store those 8 bytes and read them back later. As long as you put them back in the same order you stored them it will work.

Just for example, tested:

float restored = 0.0, dataPoint = 1.234; // I will save dataPoint and put it back in restore

byte *bytPtr;             // this is a pointer to variable type byte -- not obvious for new members
byte saveArray[ 4 ];  // I will save the float here, 4 bytes to hold 32 bits

void setup( void )
{
  Serial.begin( 9600 );
  Serial.print( "My float is " );
  Serial.println( dataPoint, 4 );
  
  bytPtr = ( byte* ) &dataPoint; // &dataPointer gives the float-type address of dataPointer
                                               // the ( byte* ) forces the compiler to fit the address onto a
                                               // byte-type pointer. The compiler keeps track of types and
                                               // the pointer holds the address, all UNO addresses are 16-bit. 
                                               // I want it byte-type to store to a byte-type array.

  // initially, bytPtr points to the 1st byte in dataPoint
  // I will "walk through" both the array and the 4 bytes of dataPoint together
  for ( byte i = 0; i < 4; i++ )
  {
    saveArray[ i ] = *bytPtr; //  * bytPtr gives the contents of the byte addressed by bytPtr 
    bytPtr++; // bytPtr now points to the next byte in dataPoint
  }

  Serial.print( "Float now stored in saveArray = " );
  for ( byte i = 0; i < 4; i++ )
  {
    Serial.print( saveArray[ i ], HEX ); // hex is just a way to view data, the bits are not changed
    Serial.print( " " ); 
  }
  Serial.println( ); 
  
  bytPtr = ( byte* ) &restored;
  // initially, bytPtr points to the 1st byte in restored
  for ( byte i = 0; i < 4; i++ )
  {
    *bytPtr = saveArray[ i ];
    bytPtr++;
  }
  
  Serial.print( "My restored float is " );
  Serial.println( restored, 4 );
  
}

void loop( void )
{
}

Otherwise you store text to SD. That is bigger and slower but advantage is a file that you can read and import into most spreadsheets or databases pretty easily.

Advantage of saving and restoring as data bytes is it is far faster and needs way less code.

Why is a 32 bit float 8 bytes in memory ? I think it is 4.

And I don't think a comma is valid as a "decimal point" in C or C++, unless there is some
special "german mode" that I never noticed.

You need to decide whether you want to save "text" data in your SD card file, or "binary" data,
and understand the difference.

1 Like

Why is a 32 bit float 8 bytes in memory ? I think it is 4.

It is 4.

And I don't think a comma is valid as a "decimal point" in C or C++, unless there is some
special "german mode" that I never noticed.

The "decimal point" is language dependent.

michinyon:
Why is a 32 bit float 8 bytes in memory ? I think it is 4.

Yup, good thing I only code for 4 bytes!

And I don't think a comma is valid as a "decimal point" in C or C++, unless there is some
special "german mode" that I never noticed.

Funny. I used a decimal point and my UNO prints floats with decimal points. What comma?

I was only showing how to get at and use the bytes in a float there and I did explain differences between text and binary as readability vs speed and ease.