array of LiquidCrystal_I2C

I have two i2c lcds and they work great together. I have attempted to create an array of LiquidCrystal_I2C so as to simplify my code so I could call lcd[0].print("something") and lcd[1].print("something") instead of lcd0.print("something") and lcd1.print("something"). Although it compiles it does not work. Is there a way to make this work properly or maybe you can't create an array of such a type?

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define SYS_0_I2C_ADDR    0x3E 
#define SYS_1_I2C_ADDR    0x3F  
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcd[2] ={
     (SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin),
     (SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)
};

void setup(void)
{

  lcd[0].begin (20, 4);
  lcd[0].setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd[0].setBacklight(HIGH);
  lcd[0].home ();                   
  lcd[0].setCursor (0,0);
  lcd[0].print("0");
  
  lcd[1].begin (20, 4);
  lcd[1].setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd[1].setBacklight(HIGH);
  lcd[1].home ();
  lcd[1].setCursor (0,0);
  lcd[1].print("1");
}

void loop()
{
}

Although it compiles it does not work.

What happens when you run the program ?

Nothing displays on the screen. The following code works fine...

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define SYS_0_I2C_ADDR    0x3E  // Define I2C Address where the PCF8574A is
#define SYS_1_I2C_ADDR    0x3F  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcdSys0(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
LiquidCrystal_I2C lcdSys1(SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup(void)
{
  lcdSys0.begin (20, 4);
  lcdSys0.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcdSys0.setBacklight(HIGH);
  lcdSys0.home ();                
  lcdSys0.setCursor (0,0);
  lcdSys0.print("0");
  
  lcdSys1.begin (20, 4);
  lcdSys1.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcdSys1.setBacklight(HIGH);
  lcdSys1.home ();                  
  lcdSys1.setCursor (0,0);
  lcdSys1.print("1");
}

void loop()
{
}

I don't understand why but this works...

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define SYS_0_I2C_ADDR    0x3E  // Define I2C Address where the PCF8574A is
#define SYS_1_I2C_ADDR    0x3F  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3

#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7


LiquidCrystal_I2C lcdSys0(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
LiquidCrystal_I2C lcdSys1(SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

LiquidCrystal_I2C lcd[2] = {lcdSys0,lcdSys1};

void setup(void)
{
  lcd[0].begin (20, 4);
  lcd[0].setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd[0].setBacklight(HIGH);
  lcd[0].home ();                
  lcd[0].setCursor (0,0);
  lcd[0].print("0");
  
  lcd[1].begin (20, 4);
  lcd[1].setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd[1].setBacklight(HIGH);
  lcd[1].home ();                  
  lcd[1].setCursor (0,0);
  lcd[1].print("1");
}

void loop()
{
}

Now, try:

LiquidCrystal_I2C lcd[2] ={
     LiquidCrystal_I2C(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin),
     LiquidCrystal_I2C(SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)
};

Yes that works as well. Can you explain it all to me?

The first code you posted just has some numbers in parentheses in curly braces. What do you think the parentheses are accomplishing?

The second code should be fairly obvious.

The third code is creating two objects and then storing them in an array. Again, fairly obvious.

The last code is creating two objects on the fly, and storing them in an array.

The difference between the first and last code is that there is a function call being made in the last code, and nothing happening in the first.

PaulS:
The first code you posted just has some numbers in parentheses in curly braces. What do you think the parentheses are accomplishing?

The second code should be fairly obvious.

The third code is creating two objects and then storing them in an array. Again, fairly obvious.

The last code is creating two objects on the fly, and storing them in an array.

The difference between the first and last code is that there is a function call being made in the last code, and nothing happening in the first.

I thought maybe the numbers in parenthesis were parameters used by the type when the array was created. It seems like I'm doing this...

int x[2] = {int a=0, int b=1};

I thought maybe the numbers in parenthesis were parameters used by the type when the array was created.

I believe that (SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin) are the parameters used when calling the constructor for the LiquidCrystal_I2C class to create one object of that type. They call it a parameterized constructor.

Here's an explanation C++ Class Constructor and Destructor

New to C++ myself.

They are... but we were talking in context of creating an array of objects.

but we were talking in context of creating an array of objects.

Huh?

Based on the written tutorial what makes you think that what you wrote in your first post is the correct way to create an object ?

I coded...

LiquidCrystal_I2C lcd[2] ={
     (SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin),
     (SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)
};

...but I needed...

LiquidCrystal_I2C lcd[2] ={
     LiquidCrystal_I2C(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin),
     LiquidCrystal_I2C(SYS_1_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)
};

...just learning.

Which written tutorial do you speak of so I may read it?

but we were talking in context of creating an array of objects.

If you are going to create an array of objects, you have to call the constructor for the class.

(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)

does not call a constructor, or any function, for that matter.

LiquidCrystal_I2c(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)

does.

Which written tutorial do you speak of so I may read it?

The one that I linked in one of my posts above.

That doesn't talk about arrays of objects.

PaulS:

but we were talking in context of creating an array of objects.

If you are going to create an array of objects, you have to call the constructor for the class.

(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)

does not call a constructor, or any function, for that matter.

LiquidCrystal_I2c(SYS_0_I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin)

does.

Yeah I understand it now. I'm surprised the compiler accepted that.

That doesn't talk about arrays of objects.

NO, it doesn't. It talks about constructors which you clearly did NOT understand prior to this.

The least of your problems was arrays of objects.
Your fixation on it is astonishing.

Your fundamental problem was not knowing how to create the objects for the array.
That is what the tutorial explained.

Ok you win.

It isn't easy. I still struggle with it.
Some of the tutorials on the internet are very good and very helpful.

ieee488:

That doesn't talk about arrays of objects.

NO, it doesn't. It talks about constructors which you clearly did NOT understand prior to this.

The least of your problems was arrays of objects.
Your fixation on it is astonishing.

Your fundamental problem was not knowing how to create the objects for the array.

Could not agree more. That is why I posted on the forum.

ieee488:
That is what the tutorial explained.

Well I missed it then. Please post the lines of code that explain it.