sprintf not converting float

I am using temperature sensors and retreiving the correct values - Serial.print is displaying correctly.

I also want to capture the data to an SD card. For this, I use sprintf to create a text 'string' and pass that char array to a separate function that writes (any string it receives) to the card (function working fine). Therefore I do not want to change the 'SDWRfile' function ( if I can help it ) as it is used for other code as well.

The sprintf conversion is displaying :

Outside temperature is: 19.50 // correct value
Inside temperature is: 18.75 // correct value
760000 : Inside = ? // result of the sprintf conversion
760000 : Outside = ? // result of the sprintf conversion

The section of code that I am seeing the incorrect display from the loop function is :

  Serial.print("Reading One-Wire Sensors\n\r");
  sensors.requestTemperatures();
  float tempC1 = readTemperature(outsideThermometer);
  float tempC2 = readTemperature(insideThermometer);

    Serial.print("Outside temperature is: ");
    Serial.print(tempC1);
    Serial.print("\n\r");     //// this is displaying the correct value in Serial Monitor, so I know that tempC1 contains a correct value.

  	char buffer [100];
	sprintf (buffer, "%ld : %s %f", cMil, "Outside = ", tempC1);
	SDWRfile("datalog.txt",buffer) ;    // writes the char array 'buffer' to the file on the SD card.


/// the following function simply takes what it received, and writes it to the file -- working correctly.

void SDWRfile(char fName[],char fTXT[]) 
{
	File dataFile = SD.open(fName, FILE_WRITE);
	// if the file is available, write to it:
	if (dataFile) {
		dataFile.println(fTXT);
		dataFile.close();
		// print to the serial port too:
		Serial.println(fTXT);
	}  
	else {
		Serial.println("error opening file");
	} 
}

Yes, this is known.

AWOL:
Yes, this is known.

Thanks AWOL. Either you're telling me something you think I should know, or that :
sprintf (buffer, "%ld : %s %f", cMil, "Outside = ", tempC1);
is not correct, or that sprintf is incapable of doing the conversion that I need ?

A bit more than "this is known" would be a great help, if you wouldn't mind :slight_smile:

If you'd done a quick search*, you would have found lots of posts on this subject, even some code to get around it.

  • I got over 900 results with a single word search over at the main site.

ah, Thanks AWOL.

I did spend a few hours on google, but am not always able to find a solution, not knowing all the terminology for things. After frustration gets the better of me, I post here looking for help from the gurus.

I will go back and look again.

At least you've confirmed for me that the problem is that sprintf is unable to do this conversion, and that there is an alternate solution out there, and I also now have a direction for my hunt.

Regards

again, my Thanks, AWOL

With your direction, I found the following solution that's giving the exact result required :

  	char buffer [100];

        float FloatToInt = 0;
        int d1 = 0;
        float f2 = 0;
        int d2 = 0;

        FloatToInt = tempC2;        // example 678.0123
        d1 = FloatToInt;            // Get the integer part (678).
        f2 = FloatToInt - d1;     // Get fractional part (678.0123 - 678 = 0.0123).
        d2 = trunc(f2 * 10000);     // Turn into integer (123).
	sprintf (buffer, "%ld : %s %d.%04d", cMil, "Inside = ", d1, d2);
	SDWRfile("datalog.txt",buffer) ;

        FloatToInt = tempC1;
        d1 = FloatToInt;            // Get the integer part (678).
        f2 = FloatToInt - d1;     // Get fractional part (678.0123 - 678 = 0.0123).
        d2 = trunc(f2 * 10000);     // Turn into integer (123).
	sprintf (buffer, "%ld : %s %d.%04d", cMil, "Outside = ", d1, d2);
	SDWRfile("datalog.txt",buffer) ;