Problems printing a variable to ssd1306 display using lcdgfx or ssd1306 library

I am having problems trying to print variables to a ssd1306 display using lcdgfx or ssd1306 library by lexus2k, there's an issue closed at lcdgfx repository about problems with displaying a variable

([OLED 0,96 - problem with displaying a variable · Issue #44 · lexus2k/lcdgfx · GitHub](OLED 0,96 - problem with displaying a variable · Issue #44 · lexus2k/lcdgfx · GitHub
also this one
displaying floating point numbers · Issue #42 · lexus2k/lcdgfx · GitHub))

i tried adding the piece of code that the developer recommended which converts numbers float to string and then prints it out, but when i do that on the ssd1306 library it just gives me a question mark and on the lcdgfx i get blank space.

code for ssd1306:

ssd1306_128x64_i2c_init(); //initializer for display (ssd1306 library)

void display() //loop for controlling the display
{
ssd1306_clearScreen(); //clears the screen
//ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font8x16); //sets font size

ssd1306_printFixed(70, 0, "T=", STYLE_BOLD);

}

void printFloatNumber() //loop for printing float numbers on oled display
{
char StrT[10];

snprintf(StrT, sizeof(StrT), "%f", currentTemperature); //also tried  sprintf( str, "%.2f", temp );
ssd1306_printFixed(85, 0, StrT, STYLE_NORMAL);

}

void setup()
{
Serial.begin(115200); //starts serial communication at 115200 baud rate

attachInterrupt(INTERRUPT, isr, CHANGE);

ssd1306_clearScreen(); //clears the screen
//ssd1306_fillScreen(0x00);
ssd1306_setFixedFont(ssd1306xled_font8x16); //sets font size

}

void loop()
{
currentMillis = millis();

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

    warningLED();
    display();
    printFloatNumber();
    //ssd1306_clearScreen(); //clears the screen
}

}

code for lcdgfx:

DisplaySSD1306_128x64_I2C display(-1); //initializer for display (lcdgfx library)

void printFloatNumber() //loop for printing float numbers on oled display
{
sprintf(StrT, "%.2f", currentTemperature);
display.write(85, 0, StrT, STYLE_NORMAL);
}

void setup()
{
Serial.begin(115200); //starts serial communication at 115200 baud rate
display.begin();

attachInterrupt(INTERRUPT, isr, CHANGE); // interrupt 0 is pin 2, interrupt 1 is pin 3

display.setFixedFont(ssd1306xled_font8x16);
display.clear();

//display.printFixed(0, 0, "U=", STYLE_BOLD);
//display.printFixed(0, 16, "I=", STYLE_BOLD);
//display.printFixed(0, 32, "P=", STYLE_BOLD);
//display.printFixed(70, 0, "T=", STYLE_BOLD);

}

void loop()
{
currentMillis = millis();

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

    display();
    printFloatNumber();
    //ssd1306_clearScreen(); //clears the screen
    display.clear();
    display.printFixed(0, 0, "U=", STYLE_BOLD);
    display.printFixed(0, 16, "I=", STYLE_BOLD);
    display.printFixed(0, 32, "P=", STYLE_BOLD);
    display.printFixed(70, 0, "T=", STYLE_BOLD);
}

any idea what's wrong?

On some (most?) arduinos, sprintf doesn't natively support floats. It can be enabled, but it's disabled for memory reasons.

The alternative is to use function dtostrf (see : avr-libc: <stdlib.h>: General utilities )

Fixed by using dtostrf instead of snprintf.

Not sure why you created another, identical topic... Did you not see my reply ?

I think so. It looks like you are using a library that is not up to the job, so you might replace it with one that is.

You will incur the wrath of the powers, and rightly, if you double post or cross post. It is confusing and discourteous.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.