I have a 40x4 LCD which internally consists of 2 40x2 LCDs.
To control this LCD I have to create 2 LiquidCrystal
objects and than later on in code decide which one to use depending on which of the 4 lines I want to change.
Due to the size of my project, I split all the LCD handling off to a separate class.
To make my code as readable and easy as possible, I want to create a member method of this class, to which I can just pass line number 1-4, and have this method take care of deciding which LiquidCrystal
object to use.
LCDController.h:
#include <LiquidCrystal.h>
class LCDControllerClass
{
protected:
public:
void init(LiquidCrystal* Clcd1, LiquidCrystal* Clcd2);
private:
void _UpdateLCD(int _iLine, int _iPosition, String _strText, int _iFieldLength);
LiquidCrystal* _Clcd1;
LiquidCrystal* _Clcd2;
};
extern LCDControllerClass LCDController;
My 2 LiquidCrystal
objects are initialized in my main .ino file like so (before the setup()
loop)
:
LiquidCrystal lcd(12, 11, 7, 6, 5, 4); //declare two LCD's
LiquidCrystal lcd2(12, 10, 7, 6, 5, 4); // Ths is the second
These two objects are then passed as pointers to my LCDControllerClass
like so:
LCDController.init(&lcd, &lcd2);
The init()
member method of my LCDControllerClass
then puts these pointers in private variables like so:
void LCDControllerClass::init(LiquidCrystal* Clcd1, LiquidCrystal* Clcd2)
{
_Clcd1 = Clcd1;
_Clcd1->begin(40, 2);
_Clcd1->clear();
_Clcd2 = Clcd2;
_Clcd2->begin(40, 2);
_Clcd2->clear();
}
Then I have another private member method which should take care of updating the correct LCD object:
void LCDControllerClass::_UpdateLCD(int _iLine, int _iPosition, String _strText, int _iFieldLength)
{
LiquidCrystal* CActiveLCD;
if (_iLine > 2)
{
//DisplayLine is higher than 2, this means we need to update the 2nd LCD
LiquidCrystal** CActiveLCD = &_Clcd2;
_iLine = _iLine - 2; //Convert line number to correct line number for 2nd display
}
else
{
//LiquidCrystal** CActiveLCD = &_Clcd1;
}
/* we have to convert this number
to a real display line (0-1) by substracting 1 again
*/
_iLine = _iLine - 1;
CActiveLCD->setCursor(_iPosition, _iLine);
CActiveLCD->print(_strText);
}
Now in this last member mehthor my code goes into the woods for some reason. The LCD is not updated at all and it seems the arduino is freezing as well since I put a small Serial.print()
test in the main loop and it's not printing anything...
What is the correct way to pass the correct LiquidCrystal
object to the CActiveLCD
object?