Go Down

Topic: Text on lcd1602 using sprintf outputs wrong (Read 116 times) previous topic - next topic

fmeersschaut

Hello,
i have defined the following :

#include <stdio.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);
char tTime[8]  ;

in the loop i have the following :
    lcd.setCursor(0, 1);
    sprintf(tTime, "%2d:%2d:%2d", myRTC.hours, myRTC.minutes, myRTC.seconds);
    Serial.write (tTime) ;
    lcd.printf(tTime);


on the serial output, everything is correct
on the lcd the time is printed correct, but just after the time, strange characters are printed.


any idea how to solve this using formatted output ?

Thx
Frederik

david_2018

tTime is too short for 8 characters, needs at least one additional space for the terminator character.  Most likely the serial display is ignoring unprintable characters, while the LCD is treating them as special characters.

marco_c

Serial.write is for one character. Serial.print is for a string.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

bperrybap

Serial.write is for one character. Serial.print is for a string.
write() in the Print class is overloaded. If passed a char * it assumes a c string.

--- bill

Go Up