# Conversion from float to string question

Dear Community,

I’m trying to convert a float value called averageLux into a string.
I tried to use this function - dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
But it doesn’t work. The string printed on the serial monitor is not correct(only display: ).

Here is the information displayed on serial monitor:

``````I received: 471.10 lux
471.10
0.00
0.00
0.00
0.00
471.10
476.10
0.00
0.00
0.00
471.10
476.10
480.40
0.00
0.00
471.10
476.10
480.40
459.30
0.00
471.10
476.10
480.40
459.30
466.00
Misson Completed!
Sum: 2352.90
Average Illuminance: 470.58
//printed string after conversion, but only display two square symbols
Maximum Illuminance: 480.40
Minimum Illuminance: 459.30
Uniormity: 1.02
``````

Here is the code regarding this specific question:

``````void AverageLux()  //Average illuminance calculation
{
float sum = 0;

for (int i=0; i<=NumOfPoints-1; i++)
{
sum += LuxValue[i] ;
}

averageLux = sum / NumOfPoints;

Serial.print("Sum: ");
Serial.println(sum);  // print average illuminance value of measurement points
Serial.print("Average Illuminance: ");
Serial.println(averageLux);  // print average illuminance value of measurement points
Serial.println(dtostrf(averageLux,2,2,9));  // print average illuminance value of measurement points
}
``````

Here is my full code:

``````#include <SPI.h>
#include <TFT.h>
#include<stdlib.h>

#define cs   10   //TFT Screen pin defintion for Arduino
#define dc   9
#define rst  8
TFT TFTscreen = TFT(cs, dc, rst);

const int NumOfPoints = 5;  // number of measurement points

char luxChars[5];
float LuxValue[NumOfPoints];  //measurement points

float maxLux;
float minLux;
float averageLux;
float uniformityLux;

int b = 0;    //variable for multiple measurement points - LuxValue array

void setup()
{
Serial.begin(9600);
Serial1.begin(9600,SERIAL_7E1); //according to the light meter communication method
delay(500);

TFTscreen.begin();
TFTscreen.background(0, 0, 0);
TFTscreen.setTextSize(1);

}

void loop()
{
delay(500);

if (b == NumOfPoints)  // Calculate Average
{

Serial.println(" Misson Completed!");
TFTscreen.stroke(255, 255, 255);
TFTscreen.text(" Misson Completed!", 40, 30);
delay(2000);
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(" Misson Completed!", 40, 30);

AverageLux();
MaximumLux();
MinimumLux();
UniformityLux();
b = 0;
}

}

{
int ndx = 0;  //variable for receivedChars array
int a = 0;    //variable for luxChars array

while (Serial1.available() > 0) //when press the D-OUT button of the light meter, read data
{
if (c != '\n')
{
ndx++;
}
else
{
for (int i=6; i<=10; i++)
{
a++;
}
LuxValue[b] = atof(luxChars);  //convert char into float and store the lux value in LuxValue Array
Serial.print(LuxValue[b]);
b++;
Serial.println(" lux");  // print illuminance unit
for (int i=0; i<=NumOfPoints-1; i++)
{
Serial.println(LuxValue[i]);   //print data stored in LuxValue array.The serial monitor shows the data are missing except the last one
}
}
}

}

void AverageLux()  //Average illuminance calculation
{
float sum = 0;

for (int i=0; i<=NumOfPoints-1; i++)
{
sum += LuxValue[i] ;
}

averageLux = sum / NumOfPoints;

Serial.print("Sum: ");
Serial.println(sum);  // print average illuminance value of measurement points
Serial.print("Average Illuminance: ");
Serial.println(averageLux);  // print average illuminance value of measurement points
delay(200);
Serial.println(dtostrf(averageLux,2,2,9));  // print average illuminance value of measurement points
delay(100);
}

void MaximumLux()   //Maximum illuminance calculation
{

maxLux = LuxValue[0];
for (int i=1; i<=NumOfPoints-1; i++)
{
maxLux = max(LuxValue[i],maxLux);
}
Serial.print("Maximum Illuminance: ");
Serial.println(maxLux);  // print Maximum Illuminance value of measurement points
}

void MinimumLux()   //Minimum illuminance calculation
{

minLux = LuxValue[0];
for (int i=1; i<=NumOfPoints-1; i++)
{
minLux = min(LuxValue[i],minLux);
}
Serial.print("Minimum Illuminance: ");
Serial.println(minLux);  // print Minimum Illuminance value of measurement points
}

void UniformityLux()    //Uniformity calculation
{

uniformityLux = maxLux/averageLux;
Serial.print("Uniormity: ");
Serial.println(uniformityLux);  // print Uniformity value of measurement points

}
``````

That's not how you use dtostrf(). Here's the documentation: http://www.atmel.com/webdoc/avrlibcreferencemanual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html

Yea you need to create a char buffer to put the contents into:

``````char buffer[16];
// The following will have a minimum of 1 digit, 2 digits after the decimal
// point, and will be dumped into the char buffer
dtostrf(averageLux, 1, 2, buffer);
Serial.println(buffer);
``````