LiquidCrystal lcd(...) functionality

Hi, maybe I was wrong posting it in the LCD forum part :roll_eyes:
Anyway, I got no answer there...

Ulli:
Hi @all,
is there anybody who can explain me the real functionality of that function? As far I understand

 // select the pins used on the LCD panel

LiquidCrystal lcd(3, 2, 4, 5, 6, 7);



is a kind of standard (I use that pins and it also works in reality).

Now I tried in the declaration part to do it like this:


uint8_t lcd1 = 3;
 uint8_t lcd2 = 2;
 uint8_t lcd3 = 4;
 uint8_t lcd4 = 5;
 uint8_t lcd5 = 6;
 uint8_t lcd6 = 7;
 LiquidCrystal lcd(lcd1, lcd2, lcd3, lcd4, lcd5, lcd6);



It also works, even when the lcd1..6 aren't constants...

So I intended to store these 6 values in EEPROM and get their values from there later on. I just wanted more flexibility because I have different hardware using different Arduinos (Uno, Nano) in different configs.
But it doesn't work if I initialize the values later (or just modify the predefined ones). Using `LiquidCrystal(...)` in the `setup()` function doesn't work either, because then it's unknown in `loop()`.
Is there any possibility to use such a flexible configuration?

Using LiquidCrystal(...) in the setup() function doesn't work either, because then it's unknown in loop().

It would be unusual to do it, but can you create the LCD instance in loop() ?

Now I tried

  uint8_t lcd1 = 2;  // wrong value for testing overwrite
  uint8_t lcd2 = 3;  // wrong value for testing overwrite
  uint8_t lcd3 = 4;
  uint8_t lcd4 = 5;
  uint8_t lcd5 = 6;
  uint8_t lcd6 = 7;
  boolean runOnce = true;
 
void loop()
{
  LiquidCrystal lcd(lcd1, lcd2, lcd3, lcd4, lcd5, lcd6);
  if (runOnce){
   lcd1 = 3;         // load the right value
   lcd2 = 2;        // load the right value
   lcd.begin(16, disp_zeilen);              // start the library
   lcd.createChar(1, la);  // upload 3 user chchars
   lcd.createChar(2, ra);
   lcd.createChar(3, bell);
   runOnce = false; 
  }
...

It seems to work, but the LCD flickers. :~

  LiquidCrystal lcd(lcd1, lcd2, lcd3, lcd4, lcd5, lcd6);Do this just once, not every time through loop().

But how? In pascal I would look if lcd^ equals nil. How do I the same in C++?

Set a boolean variable, let's call it lcdDone, to false. Test lcdDone before creating the instance. Create the instance if lcdDone is false and set lcdDone to true. Next time through loop() lcdDone will be true and a new instance will not be created.

If I do it so

  uint8_t lcd1 = 2;
  uint8_t lcd2 = 3;
  uint8_t lcd3 = 4;
  uint8_t lcd4 = 5;
  uint8_t lcd5 = 6;
  uint8_t lcd6 = 7;
  boolean runOnce = true;
 
void loop()
{
//  LiquidCrystal lcd(lcd1, lcd2, lcd3, lcd4, lcd5, lcd6);
  if (runOnce){
  LiquidCrystal lcd(lcd1, lcd2, lcd3, lcd4, lcd5, lcd6);
   lcd1 = 3;
   lcd2 = 2;
   lcd.begin(16, disp_zeilen);              // start the library
   lcd.createChar(1, la);
   lcd.createChar(2, ra);
   lcd.createChar(3, bell);
   runOnce = false; 
  }
...
  lcd.setCursor(0,0);

I get "lcd" was not declared in this scope when the compiler comes to the lcd.setCursor()
But it seems to get into the right direction.

That looks like a dead end then.

Ulli:
But how? In pascal I would look if lcd^ equals nil. How do I the same in C++?

My question meant if I could find out if lcd is not yet defined. If it wouldn't I could do the initialization only once.

Finally, I found a way to do it by myself. So I proudly present it to all of you, folks :wink:

#include <LiquidCrystal.h>

void setup(){}
uint8_t l1, l2, l3, l4, l5, l6 = 0;
boolean runOnce, printOnce = true;
LiquidCrystal lcd(l1, l2, l3, l4, l5, l6);
void loop(){
  if (runOnce){
    l1=8; l2=9; l3=4, l4=5; l5=6;l6=7;
    LiquidCrystal lcd1(l1, l2, l3, l4, l5, l6);
    lcd = lcd1;
    lcd.begin(16,2);
    runOnce=false;
  }
  if (printOnce){
    lcd.print("hello world");
    printOnce=false;
  }
}

So here is the whole thing with the LCD pins stored in the EEPROM 8)

#include <EEPROMex.h>
#include <LiquidCrystal.h>

const int memBase    = 0;
uint16_t EEVer         = memBase;     // Version in 16 bit
uint16_t EELcdRS       = memBase + 2; // --> LCD pin RS
uint16_t EELcdEN       = EELcdRS + 1; // --> LCD pin EN
uint16_t EELcdD4       = EELcdEN + 1; // --> LCD pin D4
uint16_t EELcdD5       = EELcdD4 + 1; // --> LCD pin D5
uint16_t EELcdD6       = EELcdD5 + 1; // --> LCD pin D6
uint16_t EELcdD7       = EELcdD6 + 1; // --> LCD pin D7

uint8_t  LcdRS,       // LCD pin RS
         LcdEN,       // LCD pin EN
         LcdD4,       // LCD pin D4
         LcdD5,       // LCD pin D5
         LcdD6,       // LCD pin D6
         LcdD7;       // LCD pin D7
         
LiquidCrystal lcd(0,0,0,0,0,0);

void setup(){
  LcdRS = EEPROM.readByte(EELcdRS);    // LCD pin RS
  LcdEN = EEPROM.readByte(EELcdEN);    // LCD pin EN
  LcdD4 = EEPROM.readByte(EELcdD4);    // LCD pin D4
  LcdD5 = EEPROM.readByte(EELcdD5);    // LCD pin D5
  LcdD6 = EEPROM.readByte(EELcdD6);    // LCD pin D6
  LcdD7 = EEPROM.readByte(EELcdD7);    // LCD pin D7
  
  LiquidCrystal lcd1(LcdRS, LcdEN, LcdD4, LcdD5, LcdD6, LcdD7);
  lcd = lcd1;  // <-- this is the matchwinner!                

  lcd.begin(16,2);

  lcd.print("hello world");
}

void loop(){}