5 years, 8 months ago.

How can I display a text and the value of a variable on an LCD display?

I have a 16x2 LCD display, in one of the lines I want to show something like this:

"Sequence: 3"

Where sequence would be a text and 3 would be a variable that is entered by a DIP switch of three positions.

Thanks for your reply

It depends entirely on the LCD you are using. If you can display text strings on it then you can use sprintf to create a string with the number in it.

Reading the switch value into a variable is just a case of adding numbers up.

Your code would end up as something like this

char displayText[16];
int value = readSwitches();
sprintf(displayText,"Sequence: %d",value);
lcd.display(displayText);
posted by Andy A 11 Mar 2019

1 Answer

5 years, 8 months ago.

Start by selecting one of the available LCD software components here. For example, my library, allows you to use printf on the LCD:

#include "mbed.h"
#include "TextLCD.h"
 
// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx
 
// I2C Communication
I2C i2c_lcd(p28,p27); // SDA, SCL
 
// SPI Communication
SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK
 
//TextLCD lcd(p15, p16, p17, p18, p19, p20);                // RS, E, D4-D7, LCDType=LCD16x2, BL=NC, E2=NC, LCDTCtrl=HD44780
//TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD20x4); // I2C bus, PCF8574 Slaveaddress, LCD Type
//TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2, TextLCD::WS0010); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type
TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type
//TextLCD_I2C lcd(&spi_lcd, p8, TextLCD::LCD24x4D); // SPI bus, CS pin, LCD Type
 
int dip;

int main() {
    pc.printf("LCD Test. Columns=%d, Rows=%d\n\r", lcd.columns(), lcd.rows());

    dip = 3; // read this from some DigitalIn pins
    
    lcd.printf("Sequence: %d\n\r", dip);      
}