Passing a specific I2C LCD to a function

All.

I do OK at programming and sort most issued out, that's how I learn. I think this is a pointer thing, to be honest they fry my brain!

I am using the LCD_IC2.h library.

I have two 2x16 displays, and I can print OK to either or both, What I need to do is drop power to the LCD when it is asleep via an external transistor, that works just fine. So does my function if I have only 1 LCD. What do I need to do , ehat pointer or data type to pass in the lcd objects?

Code excerpts:
#include <LCD_I2C.h>

LCD_I2C lcd1(0x27, 16, 2);
LCD_I2C lcd2(0x3F, 16, 2);

Function Definition
// User Defined Functions
bool lcdPower(int powerPin, bool state) {
// turn on/off electrical power to lcd module
if(state){
digitalWrite(powerPin, state);
lcd.backlight();
lcd.display();
lcd.clear();
} else {
lcd.clear();
lcd.noBacklight();
lcd.noDisplay();
digitalWrite(powerPin, state);
}
return state;
}

Function call
lcdActive = lcdPower(lcdPowerPin, false);
}

This works fine for 1 LCD. How do I pass in more than 1 as a third parameter?

As Shakespeare said, "That is the question".

I don't expect you to provide a code specific answer, just give me pointers and guidance so I can fix it myself and learn "how and why" it works.

[Sorry I couldn't find a more specific Forum Category to post this in.]

Rob

nevermind. Bye.

Ok, but other than moaning at a newbie can you help!

1 Like

Pass pointer to lcd as function parameter:

bool lcdPower(LCD_I2C *lcd_handler, int powerPin, bool state) {
// turn on/off electrical power to lcd module
if(state){
digitalWrite(powerPin, state);
lcd_handler->backlight();
lcd_handler->display();
lcd_handler->clear();
} else {
lcd_handler->clear();
lcd_handler->noBacklight();
lcd_handler->noDisplay();
digitalWrite(powerPin, state);
}
return state;
}

Example function call

lcdActive = lcdPower(&lcd1, lcdPowerPin, false);

Programming Questions would be suitable I can move it or you can.

Thank you! Three days of reading C++ coding books and stuff, and I still couldn't sort it, mind you I an 65 and retired, so I am a bit slow.

At least yo posted a useful answer and not just a gripe about my newbie posting style.

I will try and apply it to my code and see how it goes.

Rob

If you could that would be great thank you.

Rob

Done. :grinning:

Will be around to help if needed. :sunglasses:

Thanks.

It seems all I was missing was the LCD_IC2 object dname/type in the function definition.

Lesson learned, and duly noted.

Again thank you.

Rob

Thanks to all, except for the unhelpful post/moan about posting style. Be more positive and helpful. Don't just jump down hard on newbie errors.

Rob

1 Like

Yo need to be more positive, not jus point out issues !

Welcome to the forum.

You have a problem, we want to help for free. That often means that we like to see the full code between code tags and a photo and schematics and links to the used libraries. This is not a Question and Answer forum, we can point out problems to help to make your project work.

You might be using this library: https://github.com/blackhack/LCD_I2C

Where is the call to lcd.begin(); after powering it up again ?

Have you measured the power with the backlight turned off ? If that is ten times lower than the leakage current of the battery, then you might ignore it.

Did you know that it can be hard to get a display out of a bad state ? It can, for example, be in 8-bit mode or 4-bit mode. Good libraries deal with that.

All.

Thank you so much it works just fine.

Rob

Thanks.

As for the lcd.begin() call, it seems to work ok, but now you mention it I think it should be there as it seems more than reasonable. Code amended.

As for current consumption yes I have measured it and as I am using the narcoleptic library, with one lcd, the current saving is 98% over an average 7 day period.

Rob

I hope that answers your question.

How are you yanking power?
Are you just removing power from the VCC in of the backpack?
Or are you leaving the backpack powered and yanking power from just the LCD?

It is more complicated that just yanking power and re-applying it.
There are hardware and s/w issues that have to be addressed.

The code you have won't work if you are really removing power from the LCD.
It is more complicated than just yanking the power the LCD or backpack and re-applying it.

Things like ensuring that you don't violate the PCF8574 or LCD electrical specs when yanking power the the backpack but leaving the i2c signals still powered.
Or if PCF8574 is still powered but LCD is not powered.
i.e. you can get back currents and voltage leakages that violate the chip specs if signals connected to the chips sill have power on them but the chip itself is no longer powered.

On the s/w side, if you really do yank power from the LCD, it will need to be re-initialized when power is re-applied since the LCD will revert back to its default powerup state which is 8 bit mode. If the LCD is in 8 bit mode but the library in using bit mode, the host will no longer be able to talk to the LCD until the LCD is re-inialized to be in 4 bit mode again.

--- bill

Power is from battery; power (current( measured using a second Arduino as a data logger to SD card. Used a 7 day sample period.

Had an issue or two with the answer supplied, nothing to do with the lcd.init() method; After a bit of study and testing sorted it, how else do I learn? My fix is to change the methods from lcdHandler.clear to lcdHandler->clear() [Example] within the function. I also found that I could add the keyword "class" to the LCD_I2C definition in the function declaration; not really needed but a newbie it may help when I revisit the code in a year or so.

Anyway, thanks to everyone it'dbeen a great learning experience. Thanks for the pointers to the solution

Rob.

The power up call to lcd.begin() was only in the initialization loop, and seemed to work, but you suggestion has much merit, so in the init' stage I call the function that now has the init() method included.

Sorry for not including more code, I am still finding my feet here in this forum.

1 Like

Sorry, lost in translation (i am and a: British, and b: not a spring chicken - 65). By "yanking" I assume you mean removing power from the LCD module. For test I used the pin 13 (onboard led) to se if the transistor is in sync' with the Arduino lcd pin. Anyway the switching transistor is a 2N2222A. This is doing what I need so push it forward.

Rob

You need to help not just point out "newbie" issues. Is this to "bump" you posts? You are better than that.

A schematic would help make things clearer.

What you haven't described yet is how and where you remove/disconnect power.
As I mentioned previously, you could be removing power to the backpack, which powers the LCD or you could be leaving the backpack powered/connected to power but removing power to just the LCD.

Either way, unless you have some additional form of isolation you will be violating the chip specifications by having power going to the chips on signal pins when the chips are no longer powered.
That violates the chip specifications.
Can you get away with it? Maybe, but it is best to stick to the limits specified in the chip datasheets to avoid any potential issues and/or failures.

--- bill