I'd like to use some kind of formatting when writing to the 16x2 lcd, ála C-printf.
Is there an lcd.print("%c",65);
?
I'd like to use some kind of formatting when writing to the 16x2 lcd, ála C-printf.
Is there an lcd.print("%c",65);
?
Use sprintf() or snprintf() to create a buffer which is formatted as you require, then hand the buffer over to such library functions for display/printing
Most LCD libraries provide a class that inherits from the Print class. On ARM and ESP32 / ESP8266 based Arduino boards the Print class does implement printf().
@gfvalvo: "Most LCD libraries provide a class that inherits from the Print class. On ARM and ESP32 / ESP8266 based Arduino boards the Print class does implement printf()."
Obviously the one (LiquidCrystal) does not. What would it help if the Print class implements printf() when I cannot use it in LiquidCrystal?
There's no what to know since you didn't specify the library you're using, did you? Please provide a GitHub link to the exact library.
You've provided no evidence that it can't be used with that library.
What Arduino board are you compiling for?
If you want a character, try:
lcd.print((char)65);
or
lcd.write(65);
Note: On the basic Arduinos (UNO, Nano, Mega...) the 'float' format (%f) is not supported in sprintf() because it would make sprintf() very large.
definitely!
that same string can be sent to the serial monitor for debugging
Keep in mind that there is a very large overhead when using sprintf() or its derivatives. For example:
void setup() {
char buff[50];
// char temp[10];
int a = 5, b = 10, c;
c = a + b;
sprintf(buff, "Sum of %d and %d is %d", a, b, c);
// strcpy(buff, "Sum of ");
// itoa(a, temp, DEC);
// strcat(buff, temp);
// strcat(buff, " and ");
// itoa(b, temp, DEC);
// strcat(buff, temp);
// strcat(buff, " is ");
// itoa(a + b, temp, DEC);
// strcat(buff, temp);
Serial.print(buff);
}
void loop() {}
In the code above, the sprintf() version uses 25380 bytes of flash and 5016 of SRAM. The commented out code, while it is longer source code, it compiles to 10896 and 2968. If memory is getting tight, use the str()* or mem()* functions. The sprintf() function often has a lot more functionality than most people use, hence the code bloat.
why not use it if there isn't a memory issue?
There's no reason not to use it when memory's not a problem. I just wanted new coders to be aware that alternatives exist when memory is scarce.
what's the largest program you've written on an arduino? how many bytes?
when do you need to worry about code space?
Arduino Nano, LiquidCrystal Arduino built in 1.0.7 from the Library manager, it says.
FWIW, the sprintf version uses 4946 bytes (16%) of program memory, 215 bytes of variables. The
(char)65 (cast) version uses 3408 bytes (11%), 213 bytes for variables.
Since this is my first program on Arduino it is the largest one
OK, thanks. On a Nano (AVR Architecture), the Print class does not implement printf(). That's why I asked you to specify the board, it's important. On a Teensy 3.2 for example, Print does implement printf(). So you could do this:
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
uint8_t t = 65;
lcd.begin(16, 2);
lcd.printf("t = %d", t);
}
void loop() {
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.