Pages: [1]   Go Down
Author Topic: lcd.createChar parameters  (Read 1127 times)
0 Members and 1 Guest are viewing this topic.
East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4247
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley

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);
    }
  }
}






Logged

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8822
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this:
Code:
// 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);
    }
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4247
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4247
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8822
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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};
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4247
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John, that works a treat and what's more I understand it !
Logged

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

Pages: [1]   Go Up
Jump to: