LCD I2C is not working when initiated from an object (i.e. not from the main script)

Hi,
Checking the different issues already mentioned in the forum, I didn’t find any corresponding to mine so I created this one. It’s long because it’s accurate :grin:.

CONTEXT
Using Arduino Uno R3,
Using Grove-LCD RGB Backlight V4.0,
Using Usual Arduino IDE in Windows10
Connecting the lcd using the Arduino Uno (SCL, SDA, 5V, GND) pins via a personal shield. (not involved in the issue, but I precise it nevertheless).

CASE 1:
In usual main object.ino (i.e. containing the usual setUp() and loop() functions), I code the usual lines regarding the lcd :
rgb_lcd lcd; //->in global part
lcd.begin(16, 2); //in setUp() function
etc…
==>There, the LCD is working fine as expected, no issue to be mentioned in that case.

CASE 2:
In that second case, No code regarding the LCD in main object.ino this time.
But the usual code lines are now placed in a Class aside (whose code is in the same directory as the main object).
(An object is instantiated from that class in the main object with new operator. No originality here)
In that Class, I tried different options :
-lcd.begin(16, 2) in the object constructor
-lcd.begin(16, 2) in a dedicated function triggered from the main object,
-lcd->begin(16, 2) (instantiating beforehand the lcd with new operator)

==>The result is always the same: the LCD displays:
-a first line completely dark (but we can barely distinguish nevertheless, the first line which was supposed to be displayed by my code),
-a second line completely empty (whereas a second line is expected).
-(Backlight seems working properly).

CASE 3:
It’s a mix between CASE1 and CASE2:
-The usual code is placed in the main object, up to the “lcd.begin(16, 2);” instruction,
-The remaining part of the lcd code is placed in the Class aside using a second object “lcd2”
(where no lcd2.begin(16,2); is triggered).

==>Here, the LCD is working fine also.

QUESTION:
Is there a way to do the “lcd.begin(16, 2)” in the Class aside (and keep the LCD working of course)?
(because the case3 makes me instantiating 2 LCD objects (lcd and lcd2) which is quit impacting the SRAM occupied).
Thanks for having read that issue description up to its end :grin:

You seem to know a lot about C++, so I suggest you look at the source code and see how lcd class works.

Hi Boazhu,

Not so competent in C++ actually (I’m a french java developper to begin with).
And yes, I already tried looking at the lcd code, adding some logs, to see what was going wrong in the begin() function when triggered from an object, but unsuccessfully.
I’m not familiar neither with wire class.
That’s why I’m asking for your help.

I’m currently stuck because I’m currently exceeding the SRAM capacity (2Ko) and the workaround consisting in instanciating 2 lcd objects is no longer an acceptable solution.

Kind regards,
Jérôme