I am using a TFT to display the temperature read off a DHT22. I have to use dtostrf() to get it to actually print; here's the relevant code used to gather the readings.
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
And here's what I have that prints the temperature onto the LCD just fine.
TempF is only 2 characters long. This isn't enough to fit even the shortest decimal number.
dtostrf will fill your buffer with what is known as a c-string or null terminated character array (some research topics). So you need to have enough room for this null character also.
A String can accept c-strings (when used correctly), however why not just use an additional print statement and 'print' the 'F' directly.
Tip: if your buffer is longer than what you need, you can just add the F and replace the null.
//once you have TempF receiving the temp string correctly:
char pos = strlen( TempF );
TempF[ pos ] = 'F';
TempF[ pos + 1 ] = '\0';
pYro_65:
TempF is only 2 characters long. This isn't enough to fit even the shortest decimal number.
dtostrf will fill your buffer with what is known as a c-string or null terminated character array (some research topics). So you need to have enough room for this null character also.
A String can accept c-strings (when used correctly), however why not just use an additional print statement and 'print' the 'F' directly.
Tip: if your buffer is longer than what you need, you can just add the F and replace the null.
//once you have TempF receiving the temp string correctly:
char pos = strlen( TempF );
TempF[ pos ] = 'F';
TempF[ pos + 1 ] = '\0';
Weird, the temperature displays just fine with it set to 2. I'll increase it and see what happens. I do have it set to an independent print at the moment, but the font I'm using is not uniform so depending on the digits displayed, it can either be too far from the numbers or overlapping.
majhi:
Weird, the temperature displays just fine with it set to 2. I'll increase it and see what happens. I do have it set to an independent print at the moment, but the font I'm using is not uniform so depending on the digits displayed, it can either be too far from the numbers or overlapping.
From the reference for dtostrf:
The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.
The string is still created in RAM, however you only have allocated 2 of the bytes for use, who knows what the other bytes of memory belonged to.
If you create another variable after the declaration of TempF, say: int test = 0; then after calling dtostrf print the value of test and see if the value is still zero, or if you've overwritten its memory.
Alright, I increased my array to 6 (two digits, decimal point, one digit, F, null) and tried pyro's code. It compiles and uploads just fine, but there's no change from the original "xx.x" tempF reading.
Delta_G:
That happened by accident and isn't something you can count on.
What PaulS was alluding to was the second argument to dtostrf. That 6 means it writes at least 6 characters in the string, plus a terminating null. Since the array is only 6 elements wide it won't fit.
How many characters do you need to print in there? How do you want the temperature displayed. If you want two digits before the decimal and one digit after, plus the decimal itself, then that second argument only needs to be 4. If you use a larger number, then it will pad the front of it with spaces so strlen gets the whole 6 and when you write the F you'll write off the end of the array. If you want to add the F, then that second argument will always have to be at least 2 smaller than the size of the array.
I have 5 characters (two digits, decimal, digit, the letter F, null), so 6 characters including null. For example, "87.3F"
Oh my god, I feel like such an idiot. I had edited out the letter "F" in my font file because I anticipated using only digits and decimal. Edited the "F" back in and what do you know, it works now. This is embarrassing. /facepalm
Instead of making a buffer to build a text string to print it,
try just printing the parts you would build the string from.
Serial transmits 1 char at a time (using 10 bits, 1 at a time; 1 start bit, 8 data, 1 stop) with no time requirement between chars. Serial is a channel to pour data into. You don't have to print whole lines or even words at once, that's just the way some people learned and never really thought about.
print( sensorRead );
print( F( " label" ));
comes out the same as build string then print string. Print can't send characters as fast it gets filled.