Hi Everyone,
My first post but I have spent plenty of time reading here previously! I am currently reading the value from a float value from a sensor which I am then shifting two decimal places and then converting to a string. The flow is like this:
Acquire Data
Shift two decimal places
Convert from float to String
Pad with zeros to ensure a size of 7 digits
An example of this in data form would be:
135.96 (Acquired)
13596 (shifted)
13596 (string)
0013596 (padded with zeroes)
and here is the functions that do this:
// Global variables
float ratio = 0.0;
float concentration = 0.0;
void getParticleCount()
{
//Particle sensor Code ******************************************
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
lowpulseoccupancy = 0;
starttime = millis();
}
partString = floatToString(concentration);
//Pad particle string with leading zeros to ensure correct data format
while(partString.length() != 7 && partString != "0000000")
{
partString = String("0"+ partString);
}
}
// FUNCTION - ************ Convert float to string ***************
String floatToString (float input)
{
//convert float to integar
int shiftedVal = input*100.00;
//Output wont be precisely same as original float is only an approximation
String stringOne = String(shiftedVal);
return stringOne;
}
Occasionally the number goes incorrect with something like the following where the number shifted/string values bear no resemblance to the input which never goes below zero:
390.49 (Acquired)
0-26488 (shifted)
-26488 (string)
0-26488 (padded with zeroes)
I just can't figure out where it is coming from or what is causing it...
Thanks Guys