This is one of those moments where I have just been looking at the same piece of code too long and can't see the error which must be right in front of me. I even slept on it and came back to it this morning...
Here's my code snippet:
char cInfo; float fTemp; float fHum; // Read temperature & humidity fTemp = dht.readTemperature(); fHum = dht.readHumidity(); // DEBUG Serial.print(F("T: ")); Serial.print(fTemp); Serial.print(F(" - H: ")); Serial.println(fHum); // Format output sprintf(cInfo, "%02u/%02u/%4u,%02u:%02u:%02u,%.2f,%.2f", day(), month(), year(), hour(), minute(), second(), fTemp, fHum); // DEBUG Serial.println(cInfo); // Output info to file file.println(cInfo);
Normally this code should be used to print out the current date, time, temperature and humidity as a comma-separated list to a file. But when I looked at the file the temperature and humidity values were question marks. That's when I started adding the debug serial outputs to see. The problem happens just after the sprintf call.
The result of the above code looks like this:
T: 18.90 - H: 34.70 10/02/2013,10:26:13,?,?
The first line confirms, to me, that the temperature and humidity values are being read properly and stored in their float variables. The second line shows me what was just written to the file.
The problem appears to be in the sprintf conversion. I have tried using "%f", "%2.2f", "%2f" and "%d". That last one was a desperate measure and produced spurious numbers.
I'm sure this is something simple I'm missing... but I just can't see it.
Thank you in advance for any suggestions or directions you may point me in.