I'm writing a class to deal with LCD. As this class will be the only part of my project that writes to LCD, I decided to include everything about LCD inside it.
I can initialize the LCD and write to it but just while in the same method. Once I try to access LCD from another method it doesn't work any more. Looks like it lost the instance.
Down bellow is my code. What can I do to fix it?
Sketch:
#include "DisplayLCD.h"
DisplayLCD myDisplayLCD("myProject");
void setup() {
myDisplayLCD.init();
}
void loop() {
}
DisplayLCD.h
#ifndef my_display_lcd_h
#define my_display_lcd_h
#include "Arduino.h"
#include "LiquidCrystal.h"
class DisplayLCD {
private:
String firstMessage;
LiquidCrystal *lcd;
public:
DisplayLCD(String firstMessage);
void init();
};
#endif
DisplayLCD.cpp
#include "DisplayLCD.h"
DisplayLCD::DisplayLCD(String firstMessage) {
this->firstMessage = firstMessage;
pinMode(45, OUTPUT); analogWrite(45, 128); // brightness
pinMode(44, OUTPUT); analogWrite(44, 128); // contrast
LiquidCrystal liquidCrystal(33, 32, 34, 35, 36, 37);
lcd = &liquidCrystal;
lcd->begin(16, 2);
lcd->clear();
lcd->print(firstMessage);
}
void DisplayLCD::init() {
// lcd->begin(16, 2);
// lcd->clear();
// lcd->print(firstMessage);
}