Hello everyone. I'm fairly new to programming (and even newer to making classes for the Arduino) so please be gentle.
I've googled this issue and the closest match I was able to find to my particular problem is an older Arduino forum post found here: Class constructors with parameters - Syntax & Programs - Arduino Forum
My problem.
I am building a new LCD class which itself uses the LiquidCrystal class. It's basically a bolt-on which allows me to split a 2 x 16 character LCD screen in to four quadrants and print up to 8 chars per quadrant. It will have 4 functions in this class, one for each quadrant.
The class will initialise an instance of the LiquidCrystal class called 'lcd' (that is part of the default Arduino libarary) from within the constructor. Inside the constructor it seems to be able to use 'lcd' just fine. But when I put in code to use 'lcd' in a different function [inside the same class] then it gives me the following error:
C:\blah\LCDscreen.cpp: In member function 'void LCDscreen::q1(String)':
C:\blah\LCDscreen.cpp:28: error: '((LCDscreen*)this)->LCDscreen::lcd' does not have class type
Line 28 is the line that contains lcd.setCursor(i,0); as seen in below code snippet.
The code for the class I made is below:
LCDscreen.cpp
#include "Arduino.h"
#include "LCDscreen.h"
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <LiquidCrystal.h>
LCDscreen::LCDscreen(uint8_t rs, uint8_t rw, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) {
LiquidCrystal lcd = LiquidCrystal(rs, rw, enable, d0, d1, d2, d3); // Pins used for LCD screen
lcd.begin(16, 2); // rows, columns. use 16,2 for a 16x2 LCD, etc. This is statically set and cannot be changed
lcd.clear();
}
// The screen is split in to four quadrants. 1,2,3,4 (top left, top right, bottom left, bottom right)
// each quadrant contains one piece of information
// Default layout is 'action' in Q1. Q2 is empty. Q3 has time left. Q4 has pressure.
void LCDscreen::q1(String strContent) {
strContent.trim(); // Remove any white space if exists
for (int i = 0; i < 8; i++) {
lcd.setCursor(i,0);
//lcd.print(" ");
}
//lcd.setCursor(0,0);
if (strContent.length() > 8) {
char charBuf[50];
strContent.toCharArray(charBuf, 50);
String newString;
for (int i = 0; i < 8; i++) {
newString += charBuf[i];
//lcd.print(newString);
}
} else {
//lcd.print(strContent);
}
}
LCDscreen.h
#ifndef LCDscreen_h
#define LCDscreen_h
#include "Arduino.h"
#include <LiquidCrystal.h>
#include <inttypes.h>
#include "Print.h"
class LCDscreen
{
public:
LCDscreen(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
void q1(String);
void q2(String);
void q3(String);;
void q4(String);
LiquidCrystal lcd(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
private:
String strContent;
char charBuf[];
String newString;
int i;
};
#endif
You'll notice in LCDscreen.cpp I've commented out any calls to the 'lcd' variable outside the constructor (eg. //lcd.print(" ");). This is so that the error doesn't repeat itself. If I uncommented those lines then I would get the same error multiple times, one for each call to 'lcd' irrespective of what it is.
Could someone please point me in the right direction here? I notice that in the older forum thread (that I linked at the beginning of my post) it mentions using begin(). I tried using lcd.begin(16, 2); inside the q1 function, but this changed nothing.
Thanks in advance for your help.