Go Down

Topic: Mysterious problem with library (Read 490 times) previous topic - next topic

_kubaj

Hello, I'm trying to make library needed for my project. That library is working with I2C LCD.
When I put code only in sketch, it's working brilliantly
Code: [Select]

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
 
  lcd.begin(16,2);

  for(int i = 0; i< 3; i++)
  {
    lcd.noBacklight();
    delay(250);
    lcd.backlight();
    delay(250);
  }

}

void loop()
{
 

}

Next step was make library. In constructor  LCD would blink 3 times, but it's no working. I've tried write something on display, but it's not working too. So here is header:
Code: [Select]

#ifndef Alertino_h
#define Alertino_h

#include "Arduino.h"

#include "Wire.h"
#include "LiquidCrystal_I2C.h"

class Alertino {
public:
Alertino(int pocetCidiel);

private:
int mPocetCidiel;
LiquidCrystal_I2C lcd;

};

#endif

Here is source:
Code: [Select]

#include "Alertino.h"

Alertino::Alertino(int pocetCidiel): lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE)
{

lcd.begin(16,2);
lcd.noBacklight();
lcd.backlight();

}

And sketch for use with lib:
Code: [Select]

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "Alertino.h"

Alertino alertino(1);

void setup() {
 
}

void loop() {
 
}

Hackscribble

Hi _kubaj

On the Arduino, object instances may be created before the Arduino hardware has been initialised.  Therefore, it is dangerous to put anything hardware-related into the constructor:

Code: [Select]
Alertino::Alertino(int pocetCidiel): lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE)
{

lcd.begin(16,2);
lcd.noBacklight();
lcd.backlight();

}


Instead, add a method called begin() to your class, move the lcd commands into it, and call begin() in setup().

You may also need to keep your LiquidCrystal_I2C object as a global and pass a reference to it into your class constructor. 

Regards

Ray
Hackscribble.  Writing about making things
hackscribble@outlook.com

Hackscribble

Try this ... compiles but not tested.

Header:
Code: [Select]

#ifndef Alertino_h
#define Alertino_h

#include "Arduino.h"

#include "Wire.h"
#include "LiquidCrystal_I2C.h"

class Alertino
{
public:
  Alertino(LiquidCrystal_I2C& lcd, int pocetCidiel);
  void begin();
 
private:
  int mPocetCidiel;
  LiquidCrystal_I2C& _lcd;
};

#endif


Library:
Code: [Select]

#include "Alertino.h"

Alertino::Alertino(LiquidCrystal_I2C& lcd, int pocetCidiel): _lcd(lcd)
{

}

void Alertino::begin()
{
  _lcd.begin(16,2);
  _lcd.noBacklight();
  _lcd.backlight();
}


Test program:
Code: [Select]

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "Alertino.h"

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Alertino alertino(lcd, 1);

void setup()
{
  alertino.begin(); 
}

void loop()
{
 
}
Hackscribble.  Writing about making things
hackscribble@outlook.com

Hackscribble

Also untested, but this version hides the LiquidCrystal_I2C object inside your class.  Let us know if either version works for you.

Header:
Code: [Select]

#ifndef Alertino_h
#define Alertino_h

#include "Arduino.h"

#include "Wire.h"
#include "LiquidCrystal_I2C.h"

class Alertino
{
public:
  Alertino(int pocetCidiel);
  void begin();
 
private:
  int mPocetCidiel;
  LiquidCrystal_I2C* _lcd;
};

#endif


Library:
Code: [Select]

#include "Alertino.h"

Alertino::Alertino(int pocetCidiel)
{

}

void Alertino::begin()
{
  _lcd = new LiquidCrystal_I2C(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  _lcd->begin(16,2);
  _lcd->noBacklight();
  _lcd->backlight();
}


Test program:
Code: [Select]

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "Alertino.h"

Alertino alertino(1);

void setup()
{
  alertino.begin(); 
}

void loop()
{
 
}


Hackscribble.  Writing about making things
hackscribble@outlook.com

_kubaj

Thank you very much. First version 1 works as I expected :-)

Go Up