Building display class

HI,

To make my code more "simple" i want to put a lot of display lines in a class
To do this i took the morse example
but when i change this i get some errors
In the beginning of the cpp i need to do something with Qlcd::Qlcd but i dont know what

Qlcd::Qlcd()
{
    
}

void Qlcd::row1()
{
lcd.print("row 1");
delay(1000);
}

void Qlcd::row2()
{
lcd.print("row 2");
delay(1000);
}

the .h file is this one

#ifndef Qlcd_h
#define Qlcd_h

class Qlcd
{
  public:
    void row1();
    void row2();
};

#endif

what am i doing wrong?

You might try cut and pasting from: Arduino Playground - HomePage
It worked for me... 8)

Thanks, but its not woking with the lcd
and i still don't know what to write in Qlcd::Qlcd

... going on with google :wink:

The function named the same as the type is the constructor. ( 'Qlcd' is the type name, 'void Qlcd::Qlcd( void )' is the constructor ),

You don't need to put anything there, it is code that runs just before an instance of the class it created.
'void Qlcd::~Qlcd( void )' is the destructor and runs just before an instance is destroyed.

you need to declare it in the class declaration if you want to use it, 'void Qlcd();' You only provide the definition ( the bit with { } )

Thanks Pyro,

but know i still get errors with the following code

#include <Qlcd.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Arduino.h>

Qlcd qlcd();

LiquidCrystal_I2C lcd(0x27,16,2); 

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  Qlcd.row1();
}

void loop()
{
}

i really have no idea

Class declaration

#ifndef Qlcd_h
#define Qlcd_h

class Qlcd
{
  public:

    //Constructor
    Qlcd( void );

    //Destructor
    ~Qlcd( void );

    void row1();
    void row2();
};

#endif

Member function definitions

Qlcd::Qlcd()
{
}

Qlcd::~Qlcd()
{
}

void Qlcd::row1()
{
lcd.print("row 1");
delay(1000);
}

void Qlcd::row2()
{
lcd.print("row 2");
delay(1000);
}

Mwa i feel so stupid that i cannot create such a simple thing…
now i have the following error:

/Users/rogiervisser/Documents/Arduino/libraries/Qlcd/Qlcd.cpp: In member function 'void Qlcd::rowl1()':
/Users/user/Documents/Arduino/libraries/Qlcd/Qlcd.cpp:24: error: 'lcd' was not declared in this scope
/Users/user/Documents/Arduino/libraries/Qlcd/Qlcd.cpp: In member function 'void Qlcd::row2()':
/Users/user/Documents/Arduino/libraries/Qlcd/Qlcd.cpp:30: error: 'lcd' was not declared in this scope

and i did inlclude the #include <LiquidCrystal_I2C.h>

thanks…!

This isn't your fault this time, it is an Arduino blunder.

Your library includes libraries... Your sketch needs to know about these libraries too, add the lcd header to the sketch also.

EDIT: When compiling the IDE moves all files into a temp folder. It knows what to copy by what is 'included' in the sketch file ( libraries are not scanned for includes ).

I did this
and with this simple code i get the error

#include <Qlcd.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
}

void loop()
{
}

the only thing i did is including the #include <Qlcd.h>
and in the other files these are included also

The error isn’t in the sketch, it is in ‘/Users/user/Documents/Arduino/libraries/Qlcd/Qlcd.cpp’

lcd is declared in the sketch, the cpp file cannot see it. The reason for the class in your case is encapsulation. The class needs to take care of itself.

Qlcd.h

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#ifndef Qlcd_h
#define Qlcd_h

class Qlcd
{
  public:

    //Constructor
    Qlcd( void );

    //Destructor
    ~Qlcd( void );

    void row1();
    void row2();

  private:

    LiquidCrystal_I2C lcd;
  
};
#endif

Qlcd.cpp

#include "Qlcd.h"

Qlcd::Qlcd() : lcd(0x27,16,2)
{
}

Qlcd::~Qlcd()
{
}

void Qlcd::row1()
{
lcd.print("row 1");
delay(1000);
}

void Qlcd::row2()
{
lcd.print("row 2");
delay(1000);
}

Notice the initialiser list being used on the constructor to initialise the lcd var

What a great support all the way from Australia to Holland :wink:
Now i understand why my sister in low has left Holland for it...

ok but i think now the last error...

i use

  Qlcd.row2();

to print the code to the screen
but then i get again... an error:

HelloWorld_lcd_test.cpp: In function 'void setup()':
HelloWorld_lcd_test:17: error: expected unqualified-id before '.' token

Thanks!

I'm happy to help :slight_smile:

Qlcd is the type, you need to create an instance of it.

Qlcd MyNewClass;

void setup(){

  MyNewClass.row2(); 
}

YES!

Its running!

thanks for your support....!! :slight_smile: :slight_smile: :slight_smile: