hello, i made a gas detector using mq sensor and i attached the output of the sensor to a lcd display 20x4, now im trying to polish a bit the code adding fancy decorations and shit.
the problem is that each loop my lcd executes the begin function and flickers on and off the backlight for an instant. i tried solving this by putting the lcd.begin(20, 4); instruction on the setup() but it seems that makes the lcd unable to update new information, not sure why.
then i fixed it by putting the begin function on the loop cycle but that seems to make the lcd flickers everytime the loop cycle ends (the flickering is every 5 seconds that is the delay of the sensor scan and the total time of the loop duration, if i change the delay it changes the flickering ratio at exactly the same speed of the delay)
i would like to know why the lcd is flickering even when the backlighting is hardwired in the setup loop, and how to fix it, the information printed is perfectly fine and has no problem whatsoever.
#include <LCD03.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int SogliaSensore = 500;
int PinLedRosso = 2;
int PinLedBlu = 4;
int PinLedVerde = 7;
int Buzzer = 8;
int Timer = 5;
LiquidCrystal_I2C lcd (0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
pinMode (3, OUTPUT);
digitalWrite(3, 10);
pinMode (A4, OUTPUT);
pinMode (A5, OUTPUT);
pinMode (PinLedRosso, OUTPUT);
pinMode (PinLedBlu, OUTPUT);
pinMode (PinLedVerde, OUTPUT);
pinMode (Buzzer, OUTPUT);
pinMode (A3, INPUT);
Serial.begin(9600);
}
void loop() {
lcd.begin(20, 4);
lcd.setCursor(0, 0);
int SensorValue = analogRead (A3);
if (SensorValue < SogliaSensore) {
Serial.print("Sensor: Gas Quantity Detected is Safe! ");
Serial.println (SensorValue);
lcd.print("Gas Quantity");
lcd.setCursor(0,1);
lcd.print("Detected");
lcd.setCursor(0,2);
lcd.print("is Safe");
lcd.setCursor(0,3);
lcd.print(SensorValue);
digitalWrite (Buzzer, LOW);
RGB_controller (LOW, LOW, HIGH);
}
else
{
RGB_controller (HIGH, LOW, LOW);
Serial.print ("Sensor: WARNING! Gas Quantity Detected is Harmful! ");
Serial.println (SensorValue);
lcd.print("Warning: Gas");
lcd.setCursor(0,1);
lcd.print("Quantity Detected");
lcd.setCursor(0,2);
lcd.print("is Harmful!");
lcd.setCursor(0,3);
lcd.print(SensorValue);
digitalWrite (Buzzer, HIGH);
Timer = 2;
SensorValue = analogRead (A3) - Timer;
}
delay(5000);
}
void RGB_controller (int Luminosita_rosso, int Luminosita_blu, int Luminosita_verde) {
digitalWrite (PinLedRosso, Luminosita_rosso);
digitalWrite (PinLedBlu, Luminosita_blu);
digitalWrite (PinLedVerde, Luminosita_verde);
}
thanks in advance, alberto.