Display works.. sometimes

Hello! i need some help..
im making an automatic irrigation system and everthing was working fine.
But then i decided i wan to to add 2 buttons. If any of them is on (High) the system works, if not it is just waiting.

When i run it, the logic works but in some cases the display doesnt look well. It looks dimmed and washed out with out the complete word.
im using 12C library
The code:

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

int pushmanual = 1;
int botontiempo = 2;
int M_Sensor = A0;
int SensorNivelAgua = 7;
int RelayOn = 13;
int LedLevel = 11;

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup()
{
// activate LCD module
lcd.begin (16,2); // for 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear();

//PinModes
pinMode(RelayOn, OUTPUT);
pinMode(LedLevel, OUTPUT);
pinMode(pushmanual, INPUT);
pinMode(botontiempo, INPUT);
pinMode(SensorNivelAgua, INPUT);

//start Message
lcd.setCursor(0,0);
lcd.print("INICIANDO..");
lcd.setCursor(0,1);
lcd.print("V 0.1");
delay(3000);
lcd.clear();

}

void loop()
{
if (digitalRead(botontiempo)==LOW && digitalRead(pushmanual)==LOW ) //If it is not time or manual boton off

{
lcd.clear();
lcd.setBacklight(HIGH);
lcd.setCursor(0,1);
lcd.print("BOTONES OFF");
digitalWrite(RelayOn, HIGH); //pump off
digitalWrite(LedLevel, LOW); // LedLevel Off
}
else
{
lcd.setBacklight(HIGH);
int Moisture = analogRead(M_Sensor); //Read Moisture Sensor Value
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ES HORA");

if (Moisture> 700) // for dry soil
{

lcd.setCursor(10,0);
lcd.print("TIERRA");
lcd.setCursor(10,1);
lcd.print(" SECA ");
if (digitalRead(SensorNivelAgua)==LOW) //test the availability of water in storage - there is water
{
lcd.clear();
// lcd.setCursor(0,1);
// lcd.print("PUMP:ON ");
digitalWrite(RelayOn, LOW); //turns on pump
digitalWrite(LedLevel, LOW); //turns off LED
}
else
{
digitalWrite(RelayOn, HIGH); //turns off pump
lcd.setCursor(0,1);
lcd.print("SIN AGUA"); // theres no water
digitalWrite(LedLevel, HIGH); //Blinks LED
delay(1000);
digitalWrite(LedLevel, LOW);
delay(1000);

}
}
/*
if (Moisture>= 300 && Moisture<=700) //for Moist Soil
{
lcd.setCursor(10,0);
lcd.print("TIERRA");
lcd.setCursor(10,1);
lcd.print("HUMEDA");
digitalWrite(RelayOn,HIGH);
digitalWrite(LedLevel, LOW);

lcd.setCursor(0,1);
lcd.print("PUMP:OFF");
}

if (Moisture < 300) // For Soggy soil
{
lcd.setCursor(10,0);
lcd.print("TIERRA");
lcd.setCursor(10,1);
lcd.print("MOJADA");
digitalWrite(RelayOn,HIGH);
digitalWrite(LedLevel, LOW);
lcd.setCursor(0,1);
lcd.print("PUMP:OFF");
}
*/
}

}

The images:

I solved it! the reason was that the refresh rate in the code was too fast so when i added a delay it worked.

if (digitalRead(SensorNivelAgua)==LOW) //test the availability of water in storage - there is water
{
lcd.display();
lcd.setCursor(0,1);
lcd.print("PUMP:ON ");
digitalWrite(RelayOn, LOW); //turns on pump
digitalWrite(LedLevel, LOW); //turns off LED
delay(3000);

the thing is.. i dont really understand why, maybe someone can explain me ? thanks!

first you should post code in code tag only.

second, we do not know which library you are using. describe exactly what you are using.
anyhow, this looks odd to me, in most of the cases you only need one LCD library and this looks like you want to include 3:

#include <LCD.h>
#include <hd44780.h>
#include <LiquidCrystal_I2C.h>

third, you clear your display every loop, you switch on backlight again and again:

lcd.clear();
lcd.setBacklight(HIGH);

My advice is to print only when information has changed. So,
first read what your inputs are,
decide what to do,
if the decision changed compared to previous loop do your action and the update the LCD.

I noticed you are including header files from two different lcd libraries.
NewLiquidCrystal and the main header file from the hd44780 library.

<LCD.h>
This is main class header file from fm's NewLiquidCrystal library

<hd44780.h>
This is the main class header file from my hd44780 library

<LiquidCrystal_I2C.h>
This is an i/o class header file from fm's NewLiquidCrystal library

You should only include the header files from libraries you are using.
The code is currently not using the hd44780 library so there is no need to include <hd44780.h>

However, I would recommend that you consider switching to the hd44780 library

  • is easier to use since it can be installed and updated using the IDE library manager.
  • will auto locate the i2c address and auto detect the pin mappings and backlight control settings.
  • does not interfere with other LCD libraries including LiquidCrystal
  • is faster and includes some additional capabilities like long line wrapping.

Also, the api function setBacklight(dimlevel) takes a dim level argument not HIGH/LOW
HIGH is usually defined to be 1. So when you use HIGH as the dim level you are asking the library to make the backlight as dim as possible. The LCD device you have does not support dimming so you end up having the backlight on; however, if the LCD device supported dimming, the backlight would be very dim.

If you just want the turn on/off the backlight, I would recommend using backlight() and noBacklight() instead.

--- bill

This topic was automatically closed after 88 days. New replies are no longer allowed.