using lcd in a included library class

I would like to create a class that I can used to help display thing to my lcd screen.
But I get a error from my included cpp file:
External.cpp:9: error: 'lcd' was not declared in this scope

below are my 3 code files.
Please let me know how I can pass the lcd object to the 'External' class.

thanks.

hello.pde -- the main code

#include <LiquidCrystal.h>
#include "External.h"

// note I have a 16X2 lcd
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

External ext();

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
  ext.print_test();
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print(millis()/1000);
}

External.h

#ifndef External_h
#define External_h

#include "WProgram.h"

class External{
 public:
   External();
   void print_test();
};

#endif

External.cpp

#include "External.h"
#include "WProgram.h"
#include <LiquidCrystal.h>

External::External(){
   //??
}

void External::print_test(){
    lcd.setCursor(0, 0);
    lcd.print("It works!");
}

Please let me know how I can pass the lcd object to the 'External' class.

The External class needs a method, setLCD() for instance, that takes a reference to a LiquidCrystal object.

The sketch needs to call that method, after an instance of the External class is created.

The External class needs a field to hold a reference to the LiquidCrystal instance.

how would that look?

I have tried this:

-- in .h
class External{
 public:
   LiquidCrystal elcd;
   External(LiquidCrystal pelcd);
   void print_test();
};

-- in .cpp
External::External(LiquidCrystal pelcd){
    elcd=pelcd;
   //??
}

but it fails..

code, please :slight_smile:

I have tried this:

But that doesn't use references.

   External(LiquidCrystal &pelcd);

does, but DO NOT do this. I repeat DO NOT do this.

Add another method that is used to set the LiquidCrystal instance to use. Call it setLCD or begin or whatever appeals to you, but do not try to set the instance in the constructor. Your derriere will be full of teethmarks very quickly if you persist in this.

Okay I'll play around with that, so far I get a:
External.cpp: In constructor 'External::External()':
External.cpp:5: error: no matching function for call to 'LiquidCrystal::LiquidCrystal()'

code:

class External{
 public:
   LiquidCrystal elcd;
   External();
   void setLCD(LiquidCrystal &pelcd);
   void print_test();
};



External::External(){
    //elcd=pelcd;
   //??
}

void External::setLCD(LiquidCrystal &pelcd){
  elcd=&pelcd;
}

void External::print_test(){
    elcd.setCursor(0, 0);
    elcd.print("print works!");
}

Can you kindly tell me why putting it in the constructor is bad? or, give me some web reference

Can you kindly tell me why putting it in the constructor is bad?

Because that creates a dependency on the order that the constructors are called, and that is not a good thing.

External.cpp:5: error: no matching function for call to 'LiquidCrystal::LiquidCrystal()'

You'll need to add one. The LiquidCrystal people did not anticipate this need.

You'll need to add one. The LiquidCrystal people did not anticipate this need.

and I do this how?

and I do this how?

Fire up a text editor and start typing. You need to edit LiquidCrystal.h and LiquidCrystal.cpp.

You don't need to edit those. This compiles (I can't say it works but it compiles):

Hello.pde:

#include <Wire.h>
#include <LiquidCrystal.h>
#include "External.h"

// note I have a 16X2 lcd
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

External ext;

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
  ext.print_test();
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print(millis()/1000);
}

Note that it is:

External ext;

not:

External ext();

External.h is unchanged.

External.cpp has an external reference to lcd:

#include "External.h"
#include "WProgram.h"
#include <LiquidCrystal.h>

extern LiquidCrystal lcd;

External::External(){
   //??
}

void External::print_test(){
    lcd.setCursor(0, 0);
    lcd.print("It works!");
}