**Using a string to display temperature on an OLED
Hello
I just dont get it on how to properly to format a string. The goal, print temperature and humidity using a fix 3 characters spaces. I did look at Sprintf, but I dont get it.
some info:
The string is limited to 10 characters per line
Each line is 3 characters for actual temperature + 3 spaces + 3 characters for the target temperature + ("C" or "F")
Each data on a line be always aligned. If lower than 100, space to replace missing characters
Temperature value for sensor is a float
If value is lower than 100, meaning using less than 3 characters, missing number replaced by a space
What don't you get about sprintf()? There are countless tutorials on line. Example
For help with a specific problem in your code, please read and follow the directions in the "How to get the best out of this forum" post, linked at the head of every forum category.
3 digits is not enough for a floating point temperature if you account for the dot and even just one decimal and a possible negative sign.
What’s the real range of temperature, can it be -25.5°C ?
And you probably should update the screen with the right granularity and prevent unwanted blinking if you repaint the whole screen by addressing each field separately. get the value in a String without leading spaces if this is what you want and then ask the length of that String and pas the start of the string with as many spaces needed to match the field width.
If you are on AVR then sprintf by default does not handle floating point variables but if you display integers only then indeed convert the variable to an integer before
float tempC = -25.4;
…
int displayTempC = tempC;
// then use displayTempC in sprintf
sprintf() on Arduino doesn't support floats. dtostrf() can be used to format c-strings of floats that are formatted into longer strings with multiple values