It is currently Thu 23 Sep 2021 10:23 pm

All times are UTC + 7 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Arduino: How to use custom char with lcd.print()
PostPosted: Tue 23 Aug 2016 9:18 am 
Offline
Site Admin

Joined: Fri 01 May 2009 8:55 pm
Posts: 2299
Normally, the custom character will print with lcd.write() command as the example below:

Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
  byte customChar0[8] = {31,17,10,04,14,31,31,00};   // Clock
  byte customChar1[8] = {04,14,14,14,31,00,04,00};   // Bell

  // Do assign lcd.begin() before lcd.createChar()
  lcd.begin(20,4);                  // Initialize the lcd for 20 chars 4 lines
  lcd.createChar(0,customChar0);    // Clock
  lcd.createChar(1,customChar1);    // Bell
  lcd.print("Hello");
  lcd.write(byte(0));
  lcd.write(byte(1));
}


Top
 Profile  
 
 Post subject: Re: How to use custom char with lcd.print()
PostPosted: Tue 23 Aug 2016 9:32 am 
Offline
Site Admin

Joined: Fri 01 May 2009 8:55 pm
Posts: 2299
We can access the custom characters with \0 to \7 (Oct).
But the \0 is treated as a string terminator in C.
We can use an \x08 instead of a \0.
It accesses the same memory location due to 'foldback' addressing (x08-x0F).
But \x08 will probably do a backspace on a serial terminal.

Code:
lcd.print("Custom Char 0:\x08");  // Hex
lcd.print("Custom Char 1:\1");    // Oct
lcd.print("Custom Char 2:\2");    // Oct

//We have to force the expression to be evaluated as a String.
lcd.print("Hello World");
lcd.print(String("Hello") + " " + "World");
lcd.print("Hello" + String(" ") + "World");
lcd.print(String("Hello") + "\x32" + "World");
lcd.print("Hello" + String("\x08") + "World");
lcd.print("Hello" + String("\1") + "World");


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 7 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: