system
November 16, 2014, 8:50am
1
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
#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:
#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:
#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:
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "Alertino.h"
Alertino alertino(1);
void setup() {
}
void loop() {
}
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:
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
Try this ... compiles but not tested.
Header:
#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:
#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:
#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()
{
}
Also untested, but this version hides the LiquidCrystal_I2C object inside your class. Let us know if either version works for you.
Header:
#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:
#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:
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "Alertino.h"
Alertino alertino(1);
void setup()
{
alertino.begin();
}
void loop()
{
}
system
November 16, 2014, 12:58pm
5
Thank you very much. First version 1 works as I expected