Help with using LiquidCrystal in a class

Hi guys,

I'm trying instantiate an object from the LiquidCrystal class, and use the "begin()" method, but it only gives me errors. How can I do this properly?

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

class Display2{
LiquidCrystal lcd2(12,10,5,4,3,2);


void use(){
  lcd2.begin(16,2);
}

  
};

Which errors ?

Have you tried having Arduino.h before LiquidCrystal.h ? (And using angle brackets <> unless you have a local copy you want to use to show intent)

I just tried both of those suggestions, but they didn't work either.

These are the errors the compiler gives me.

 LiquidCrystal lcd2(12,10,5,4,3,2);

                    ^~

test.h:6:20: error: expected ',' or '...' before numeric constant


test.h:12:3: error: invalid use of member function 'LiquidCrystal Display2::lcd2(int)' (did you forget the '()' ?)

   lcd2.begin(16,2);

   ^~~~

Show the full error trace
What’s the other file you have for testing ? Do you have a test,cpp ?

Does it make any sense instantiating the lcd in every instance of your class ? And if there is only one instance ever created, why would the lcd be hard coded in there ?

Otherwise syntaxically just declare it’s a LiquidCrystal (pointer) typed variable and call it’s constructor in your class constructor that’s where it will really be instantiated and parameters needed

Ok here's the full error trace, and that other file is just has the #include "test.h", as well as setup(), and loop() functions are, but they don't contain anything. Also, I don't have a .cpp file.

In file included from C:\Users\airpl\Documents\Arduino\Helmet_v1\Helmet_v1.ino:2:0:

test.h:6:27: error: expected identifier before numeric constant

 extern LiquidCrystal lcd2(12,10,5,4,3,2);

                           ^~

test.h:6:27: error: expected ',' or '...' before numeric constant

test.h:6:40: error: storage class specified for 'lcd2'

 extern LiquidCrystal lcd2(12,10,5,4,3,2);

                                        ^

sketch\test.h: In member function 'void Display2::use()':

test.h:12:3: error: invalid use of member function 'LiquidCrystal Display2::lcd2(int)' (did you forget the '()' ?)

   lcd2.begin(16,2);

   ^~~~

test.h:12:3: error: invalid use of member function 'LiquidCrystal Display2::lcd2(int)' (did you forget the '()' ?)

exit status 1
expected identifier before numeric constant

And yeah your right, so what else would you recommend I put it so it doesn't create a new instance every time, and out of curiosity how could I, in code, declare it's a LiquidCrystal typed variable and call it's constructor in the class constructor.

I would have a member variable as a pointer to a LiquidCrystal object and you would instantiate the lcd in the main .ino and pass the pointer to the LCD to the constructor of your class.

Ok thank you :), but could show me how to do that in code, I've never done that before so I have no clue how to do that.

I’m on my mobile so hard to type code but something like this

#ifndef _display2_h_
#define _display2_h_

#include <Arduino.h>
#include <LiquidCrystal.h>

class Display2 {
  private:
    LiquidCrystal* _lcd;
  public:
    Display2(LiquidCrystal* aLCD) : _lcd(aLCD) {}

    // a stupid function
    size_t show(const char* s, uint8_t c, uint8_t l)
   {
      _lcd->setCursor(c,l);
      return  _lcd->print(s);
    }
};

#endif

Perfect got it :slight_smile: :slight_smile: , thank you so much for taking the time to help me out, and have a wonderful evening.

You’re welcome. Happy coding!