Problem with converting double to string using sprintf function and printing it on a display

I am having problems trying to print variables to a ssd1306 display using ssd1306 library by lexus2k, there's an issue closed at lcdgfx repository about problems with displaying a variable, for now i'm just measuring temperature it's stored as double

There are 2 closed issues similar to my question, but they didn't help me

https://github.com/lexus2k/lcdgfx/issues/44
https://github.com/lexus2k/lcdgfx/issues/42

i tried adding the piece of code that the developer recommended
1
2
sprintf(StrT, sizeof(StrT), "%f", currentTemperature);
ssd1306_printFixed(85, 0, StrT, STYLE_NORMAL);

which he said converts to string and then prints it out,
but when i do that on the ssd1306 library it just prints out random characters, also tried changing %f to several different variations that i found a reference for on a website but it didn't work and changing char size changes the output of the StrT.
This is the part of the code i'm having problems with, there are also more loops and variables, but tjose are for measuring current, voltage, readings from the encoder and i didn't include them so you can read the code more easily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Wire.h>        //library for i2c communication
#include <SPI.h>         //library for spi communication
#include "ssd1306.h"     //library for controlling ssd1306 oled display

void temperature() //loop for measuring temperature
{
    pinMode(thermistorPin, INPUT); //sets thermistorPin as input

    thermistorVoltage = analogRead(thermistorPin);                               //reads analog values from thermistorPin and writes them to thermistorVoltage
    R2 = R1 * (1023.0 / (float)thermistorVoltage - 1.0);                         //equation for calculating temperature
    logR2 = log(R2);                                                             //logarithmic function
    currentTemperature = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); //equation for calculating temperature
    currentTemperature = currentTemperature - 273.15;                            //equation for calculating temperature
    //currentTemperature = (currentTemperature * 9.0) / 5.0 + 32.0;                //for fanrenheit
}

void printFloatNumber() //loop for printing float numbers on oled display
{
    char StrT[10];
    sprintf(StrT, sizeof(StrT), "%f", currentTemperature);
    //sprintf(StrT, sizeof(StrT), "%.2f", currentTemperature);
    ssd1306_printFixed(85, 0, StrT, STYLE_NORMAL);
    //ssd1306_printFixedN(85, 0, StrT, STYLE_NORMAL, FONT_SIZE_NORMAL);
}

void setup()
{
    Serial.begin(115200); //starts serial communication at 115200 baud rate
   
    ssd1306_128x64_i2c_init(); //initializer for display (ssd1306 library)
    ssd1306_clearScreen();     //clears the screen
    //ssd1306_fillScreen(0x00);
    ssd1306_setFixedFont(ssd1306xled_font8x16); //sets font size

    ssd1306_printFixed(0, 0, "U=", STYLE_BOLD);
    ssd1306_printFixed(0, 16, "I=", STYLE_BOLD);
    ssd1306_printFixed(0, 32, "P=", STYLE_BOLD);
    ssd1306_printFixed(70, 0, "T=", STYLE_BOLD);
}

void loop()
{
    currentMillis = millis();

    if (currentMillis - previousMillis >= interval)
    {
        previousMillis = currentMillis;

        
        temperature();
        printFloatNumber();
    }

    currentMillis1 = millis();

    if (currentMillis1 - previousMillis1 >= interval1)
    {
        previousMillis1 = currentMillis1;

        Serial.println(currentTemperature);
        Serial.println(StrT);
    }
}


The output on the serial monitor of currentTemperature and StrT is
27.77
�␄2

Also tried printf, snprintf and dtostrf

I'm really not sure what's the problem, this is my first time working with a function like this, would really appreciate some help from more experienced programmers.
If you need more information i can include it.
Sorry for any typos.
Try

 
sprintf(StrT, "%f", currentTemperature);

%f is the default, 5 digits I think? if you need more:
%1.16f gives you 123.45678... format up to 16 digits after decimal
%1.2f gives you 2 after decimal
there are also codes for scientific notation if the above is not enough or your values are < 1 or whatever.
sprintf is dangerous if your values are out of bounds. Eg if you have a C style string with 20 characters and you have a very large or very small value it can be more than 20 digits in the 1.xf format. scientific is safer, it has a fixed size so you know it will fit... be aware of that.
Last edited on
Topic archived. No new replies allowed.