Go Down

Topic: lcd.createChar parameters (Read 1 time) previous topic - next topic

UKHeliBob

I am experimenting with user defined characters on an LCD and have the basics working OK.  The characters are defined in byte arrays and created using createChar with the usual parameters of character number to be created and the name of the array to be used.

I am creating characters on the fly inside a for loop.  This works and I can put up with the fact that redefining a character causes instances of it previously displayed on the LCD to be changed.  Currently the name of the array to be used in the character definition is selected using switch case based on the value of the for loop variable but this is very clumsy.  What I would like to do is to use the values of 2 nested for loop variables as the second parameter but I don't know if this is possible and, if it is, the syntax to be used.

An extra complication seems to be that the names of the arrays cannot be numeric but must must start with an alphabetic character so this will need to be concatenated to the front of the combined loop variable names.

Did I mention that I am new to C  :)

Below is an example of the sort of thing that I would like to do.  Is it possible, and if so, how ?

Bob

/*
Illustration of how I would like to define LCD
characters on the fly using loop variables as the array name
*/

#include <LiquidCrystal.h>

byte p00[8] = {
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000, 
};

byte p01[8] = {
  B01000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000, 
  B00000,
};

byte p02[8] = {
  B00100,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte p12[8] = {
  B00000,
  B00100,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte p22[8] = {
  B00000,
  B00000,
  B00100,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte p21[8] = {
  B00000,
  B00000,
  B01000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte p20[8] = {
  B00000,
  B00000,
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte p10[8] = {
  B00000,
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};

void setup()
{
}

void loop()
{
  for (int row=0; row<3;row++)
  {
    for (int col=0;col<3; col++)
    {
      //create the name of the array to be used in the definition
      //I know that the the syntax is wrong and it may not be possible to do this anyway.
      arrayName='p'+row+col;
      lcd.createChar(1,arrayName);
      lcd.setCursor(0,0);
      lcd.write(1);
    }
  }
}






Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

johnwasser

Something like this:
Code: [Select]

// 3x3 Two-dimesional array of pointers to arrays of 8 bytes.
byte (*array[3][3])[8] = {{p00,p01,p02},{p10,p11,p12},{p20,p21,p22}};

void setup()
{
}

void loop()
{
  for (int row=0; row<3;row++)
  {
    for (int col=0;col<3; col++)
    {
      lcd.createChar(1,array[row][col]);  // Value is a pointer to an array of 8 bytes
      lcd.setCursor(0,0);
      lcd.write(1);
    }
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UKHeliBob

Thanks for that John. 

I will need to sit down in a darkened room and try to understand what your code does.  Before I can try it I will need to set up the LCD on the breadboard again as I am currently experimenting with shift registers.

Bob
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UKHeliBob

Change of plan !

I have 22 user defined characters that I want to output and a single loop will do.
Using the method suggested I have tried to define the array of pointers to the character byte arrays as follows :

byte (*array[22]) [8] = {p00,p01,p02,p03,p04,p14,p24,p34,p44,p54,p64,p74,p73,p72,p71,p70,p60,p50,p40,p30,p20,p10};

I get an error: "cannot convert 'byte*' to 'byte (*)[8]' in initialization"

So, what have I done wrong ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

johnwasser

This version seems to work.  The .createChar() function doesn't need a array of 8 bytes, just a pointer to a byte.

Code: [Select]

byte *array[22] = {p00,p01,p02,p03,p04,p14,p24,p34,p44,p54,p64,p74,p73,p72,p71,p70,p60,p50,p40,p30,p20,p10};
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UKHeliBob

Thanks John, that works a treat and what's more I understand it !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up