LiquidCrystal change custom char

Hi, I want to change a custom char, mid-way through a sketch. And the reason that I don't just use another char, is that I have to use all 8 chars, and I need them to change to a lot more than 8 states.
So far, I have made this:

#include <LiquidCrystal.h> 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte customChar[8] = {
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b10101,
  0b00000
};

void setup() {
  lcd.createChar(0, customChar);
  lcd.begin(16, 2);
}

void loop() {
  lcd.write((uint8_t)0);
  customChar[8] = {
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00100,
    0b00000,
    0b10100,
    0b00000 
  };
  delay(3000);
}

But I get error:

C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_362623\sketch_jun26a.ino: In function 'void loop()':
sketch_jun26a:32:3: error: cannot convert '<brace-enclosed initializer list>' to 'byte {aka unsigned char}' in assignment
   };
   ^
exit status 1
cannot convert '<brace-enclosed initializer list>' to 'byte {aka unsigned char}' in assignment

I don't know how to fix this.

  customChar[8] =
  {
    0b00000,
    0b00000,
    0b00000,
    0b00000,
    0b00100,
    0b00000,
    0b10100,
    0b00000
  };

You cannot define all 8 elements of an array like this in a function, only when it is first declared

The easiest way to do what you want is to declare and define 2 arrays, each with the custom character definition and copy the required one to a third array that you use in the function

Something like this (untested)

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte customChar_1[8] =
{
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b10101,
  0b00000
};

byte customChar_2[8] =
{
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00100,
  0b00000,
  0b10100,
  0b00000
};

byte activeCustomChar[8];

void setup()
{
  memcpy(activeCustomChar, customChar_1, 8);
  lcd.createChar(0, activeCustomChar);
  lcd.begin(16, 2);
}

void loop()
{
  lcd.write((uint8_t)0);
  delay(3000);
}

Note that if a custom character is currently displayed on screen then changing its definition will change what is displayed even if it was previously printed with the previous definition

At this point, you have an array called "customChar", but it has only eight elements, so to assign anything to the ninth element is clearly A Bad Thing.

Just create a new array, and call createChar with it as a parameter.

To change the glyph (image) of Custom Character 0 you will have to call "lcd.createChar(0, newGlyph);" again.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte Glyph0A[8] =
{
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b10101,
  0b00000
};

byte Glyph0B[8] =
{
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00100,
  0b00000,
  0b10100,
  0b00000
};

void setup()
{
  lcd.begin(16, 2);
  lcd.write((uint8_t)0);
}

void loop()
{
  lcd.createChar(0, Glyph0A);
  delay(3000);
  lcd.createChar(0, Glyph0B);
  delay(3000);
}

If you replace a custom character in mid sketch, the new character will replace all instances of the original character when the display is updated.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.