Is there a way to use custom char with lcd.print()

#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
  lcd.begin(20,4);
  lcd.createChar(0,customChar0);
  lcd.print("Hello");
  lcd.write((byte)0);
}

Is there a way to use custom char with lcd.print() command in one go?

lcd.print(“Hello” + (byte)0); didn’t work. Sometime I need to pass to function too.

The NUL character is treated as a string terminator in C.

The String C++ class might be able to cope with it, but I would not risk it.

Your example shows a "safe" way to display the custom characters. If you are determined to use the String class to concatenate, say this:

    lcd.print("Hello" + "\1");    //risky with chr(0) i.e. NUL

Untested. But it should work with custom 1-7. If you want custom 0, use a separate print statement like your example.

David.

Arduino: 1.6.11 (Windows 7), Board: "Arduino/Genuino Uno"

LCD_Test:16: error: invalid operands of types 'const char [6]' and 'const char [2]' to binary 'operator+'

lcd.print("Hello" + "\1");

^

exit status 1 invalid operands of types 'const char [6]' and 'const char [2]' to binary 'operator+'

You have to force the expression to be evaluated as a String.

void setup()
{
    Serial.begin(9600);
    Serial.println("Hello World");
    Serial.println(String("Hello") + " " + "World");
    Serial.println("Hello" + String(" ") + "World");
    Serial.println(String("Hello") + "\x20" + "World");
    Serial.println("Hello" + String("\0") + "World");
    Serial.println("Hello" + String("\1") + "World");
}

void loop()
{
}

If you don't want to have messy statements like the above, declare one of the components as a String in the first place.

The String class is extremely bloated, but if your sketches are small it is sometimes convenient.

Note that you can NOT concatenate C char arrays. You use strcat() or sprintf() functions

David.

Thanks for teach me. It works :)

Untested. But it should work with custom 1-7. If you want custom 0, use a separate print statement like your example.

If you want to use custom character 0 you can use an 8 instead of a 0. It accesses the same memory location due to 'foldback' addressing.

Don

I showed examples of String concatenation with the Serial device. Depending on your Terminal software, it may do special things for the "non-printing" ascii characters.

If you are printing literal strings, you can just write in one go. No need for the String class.

     lcd.print("custom char 1:\1");
     lcd.print("custom char 2:\2");
     lcd.print("custom char 0:\8");    //using Don's tip

Note that chr(8 ) will probably do a BackSpace on a Serial Terminal.

David.

Thank you for all tricks again.

I am on IDE 1.6.11, \8 and \9 will show as 8 and 9 because of OCT number. So I test with \x08 and work well.

  lcd.print("\x08\x01\x02\x03\x04\x05\x06\x07");
  lcd.print("\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F");

You have found all eight 'foldback' addresses.

To understand what is happening you have to first compare the binary versions of the numbers. Here are some examples:

3 ==> 0011   4 ==> 0100   5 ==> 0101
B ==> 1011   C ==> 1100   D ==> 1101

Note that the only difference between the upper group and the lower group is the value of the highest (leftmost) bit.

It turns out that only the lower three bits are significant when accessing the memory that is allotted for those custom characters. Therefore, as far as the LCD controller is concerned, the upper group and the lower group are treated the same.

Don