Displaying character "\"

I am trying to cycle through an array that holds the characters / - \ |

I couldn't get the \ character in the array other than using consecutive \ characters because the IDE would error on that line.

When print to the serial monitor, they look fine and cycle through all four characters.

char WDC[10] = "/-\\|";                 // Create array to hold characters
int wdcCount = 0;                       // Set variable to hold array position

void setup(){
Serial.begin(115200);                   // Start Serial Monitor
}

void loop() {
  Serial.print("Print this char ");     // Print message on LCD
  Serial.print(WDC[wdcCount]);          // Print character from WDC array
  wdcCount = wdcCount + 1;              // Increment counter
  if (wdcCount > 3) { wdcCount = 0; }   // Reset Counter
}

However, when I send to a 16x2 I2C LCD they all print but instead of the \ character, I get what looks like a Y with two horizontal lines across it.

I searched but cannot find anything relating to displaying the\ character. What am I missing?

Try the following codes:

char WDC[] = {'/', '-', '\\', '\\', '|', '\0'};
Serial.print(WDC);

Output:

/-\\|

Post a link to the LCD display, or check the data sheet to see what characters it is designed to display.

When defining C-strings, the "" is an escape character, with says to take the next character or special sequence as the character to print (so you can print the double quote character, for example).

It is very strange that you did not find anything, since the facts are well known...
Backslash \ has special meaning in most program languages, it usually indicates the start of i.e "escape sequences"

Maybe the LCD doesn't have a \ character in its character generator ROM.
You could try using LCD.write to write every character code

1 Like

Typical LCD font table explains your problem:

2 Likes

Yes, the A0 Font table does not contain the backslash.

You can create a custom character with the backslash pattern and call it.

1 Like

Works fine in the serial monitor but not the LCD.

It is the same but with lcd.print instead of serial.print

I was referring to that I could not find anything about displaying the \ character.

I think you are correct.

You can always define it as one of your eight custom characters,but you'll need to handle printing separately

Yes after it was pointed out that the character does not exist it looks like adding a custom character is the only way. Sorry but I don't get what you mean by "handle printing separately"?

Well, even if you define the character it won't be in the right place in the lookup table in the LCD's ROM, so if the backlash is in a string that you want to print, you'll still print the yen symbol.

@essejcds

In my LCD library the backslash is replaced by the | as this is available on the most common character ROM set.

20230220_180739

with this code:

#include <Wire.h>                      // needed for the I2C interface
#include <NoiascaLiquidCrystal.h>      // download library from https://werner.rothschopf.net/202009_arduino_liquid_crystal_intro.htm 
#include <NoiascaHW/lcd_PCF8574.h>     // include the proper IO interface

const byte cols = 16;                  // columns/characters per row
const byte rows = 2;                   // how many rows
const byte addr = 0x3F;                // set the LCD address to 0x3F or 0x27

LiquidCrystal_PCF8574 lcd(Wire, addr, cols, rows);               // create lcd object - with support of special characters

void setup()
{
  Wire.begin();                        // start I2C library
  lcd.begin();                         // initialize the LCD
  lcd.backlight();                     // turn on backlight
  lcd.setCursor(1, 0);
  lcd.print("Hello backs \\");
  lcd.setCursor(0, 1);
  lcd.print("αβμΣ°÷∞←→äöüßÄÖÜ");     // show some special character entered in UTF-8
}

void loop() {
}

The library (for private usage) can be downloaded here:

https://werner.rothschopf.net/202009_arduino_liquid_crystal_i2c_en.htm

If you need a real \ on the LCD, see the example 5001_Convert.ino which uses a special character for the \ (and some other characters).

You can embed a special character in a c-string my using a backslash followed by the octal representation of the special character number. If using character 0, use \010 as most LCD displays take 8-15 as well as 0-7 for the same set of special characters.

Define and print the "custom character". To learn how to do that, study the LCD library documentation.

Thank you all for your input! I got it running now.

#include <Wire.h>                                                 // Load library
#include <LiquidCrystal_I2C.h>                                    // Load library
LiquidCrystal_I2C lcd(0x27, 16, 2);                               // Set LCD I2C address, columns and rows
int wdcCount = 0;                                                 // Set variable to hold array position
byte c0[8] = { B10000, B01000, B00100, B00010, B00001, B00000 };  // Custom LCD character for backslash
byte c1[8] = { B00100, B00100, B00100, B00100, B00100, B00000 };  // Custom LCD character for backslash
byte c2[8] = { B00001, B00010, B00100, B01000, B10000, B00000 };  // Custom LCD character for backslash
byte c3[8] = { B00000, B00000, B11111, B00000, B00000, B00000 };  // Custom LCD character for backslash

void setup() {
  lcd.init();             // Initialize LCD
  lcd.backlight();        // Turn on LCD backlight
  lcd.clear();            // Clear LCD screen
  lcd.createChar(0, c0);  // Write character to LCD mem
  lcd.createChar(1, c1);  // Write character to LCD mem
  lcd.createChar(2, c2);  // Write character to LCD mem
  lcd.createChar(3, c3);  // Write character to LCD mem
}

void loop() {
  lcd.setCursor(0, 0);                 // Set curson position on LCD
  lcd.print("Cust Char ");             // Print message on LCD
  lcd.print(char(wdcCount));           // Print character from WDC array
  wdcCount = wdcCount + 1;             // Increment counter
  if (wdcCount > 3) { wdcCount = 0; }  // Reset Counter
  delay(500);                          // Delay
}

Your comments suggest you have four different custom backslash characters.

The code could be much simpler with an array, as per the original post, and just one custom character.

I agree but this is going into a much larger sketch and I like having the option to easily edit the characters (I wrote a little program in vb that generates the code based on which blocks I highlight so I can just copy paste into a sketch :grimacing:) and for me easier to read since the portion in void setup will be in a function.