Keeping a steady LCD screen

So i am building part of a project for school where i am building a leak detector where i will be using xbee modules to send data to a main control unit that will display the issue and which detector. For this step i am just trying to use two buttons which have their own pin on the arduino to tell the arduino which message to send to the LCD. Ideally i want the message to be displayed when the pin is high until it goes low again. I have gotten this program to work but my problem is everytime the code runs through the loop to check it flickers so fast the message isnt visible on the lcd. I have somewhat corrected this problem by adding delays but once that delay is over i still get a small flicker. Is there anything i could do to get the lcd to stay uninterrupted?

here is my code:

#include <LiquidCrystal.h>

const int rs = 7, e = 6, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);

const int pb1 = 8;
const int pb2 = 9;


void setup() {

lcd.begin(16,2);

  }

void loop() { 
    pinMode(pb1,INPUT);
  pinMode(pb2, INPUT);

  int button1 = digitalRead(pb1);
  int button2 = digitalRead(pb2);

  
  
  
  if (button1 == HIGH) {
       
        lcd.begin(16, 2);
        lcd.print("Leak Detected");
        lcd.setCursor(3, 1);
        lcd.print("Zone 1");
        delay(1000);
  }
  else if (button2 == HIGH) {
   
        lcd.begin(16, 2);
        lcd.print("Leak Detected");
        lcd.setCursor(3, 1);
        lcd.print("Zone 2"); 
        delay(1000); 
  }
  else {
   
        lcd.begin(16, 2);
        lcd.print("System OK");
        delay(1000);
  }
}

Hi,
Welcome to the forum.

What do you want displayed;

  • With no buttons ON?
  • With Button1 ON?
  • With Button2 ON?
  • With both Buttons ON?

You only need

lcd.begin(16, 2);

in the setup.

Remove the two in the loop and that may fix your problem.
You only need to set the LCD and code up once to get it to communicate with each other.

Tom... :slight_smile:

Yes the

  lcd.begin(16, 2);

Is wiping the screen and then you are drawing on it again. That is why you see flicker.

Once you remove this you will have other problems like needing to rub out old data before you write new data. You do this by drawing a black rectangle before you write the data.

There are four possible states with two buttons.
I would probably code this as a state machine, and only write once to the LCD if the state changes.
You can use lcd.clear(); to wipe the screen before printing something else.
Leo..

You can use lcd.clear(); to wipe the screen before printing something else.

Yes but that would cause flickering.

TomGeorge:
You only need

lcd.begin(16, 2);

in the setup.

Remove the two in the loop and that may fix your problem.
You only need to set the LCD and code up once to get it to communicate with each other.

Tom... :slight_smile:

Might as well move the pinMode commands to setup as well. Once they are set they stay. No need to clutter the loop() and (uselessly) keep assigning the pin mode.

You should rewrite only those parts of the lcd that change when they change.

in pseudo code

void setup()
{
LCD.begin(16, 2);
// print fixed texts
lLCD.print("whatever:");
...
}

void loop()
{
if (something changed)
{
LCD.gotoxy(x, y);
LCD.print(newValue);
}
if (somethingElse changed)
{
LCD.gotoxy(x, y);
LCD.print(newValueForThat);
}
...
}

code tags ommited on purpose

Grumpy_Mike:
Yes but that would cause flickering.

I see 'flickering' as re-writing the same text over and over with each loop.
With a state machine, new text is written once when a button is pressed or released.
Not with every loop().

You can also print new text with extra spaces that cover the old text, like this...
lcd.print("System OK "); // 13 characters to cover "Leak Detected"
Leo..

Yes I would agree with that. Writing blanks is a good solution for an LCD, it woudn't work for an OLED as that dosn't alter the display just writing spaces.

Grumpy_Mike:
Yes I would agree with that. Writing blanks is a good solution for an LCD, it woudn’t work for an OLED as that dosn’t alter the display just writing spaces.

im not having a problem with covering up the words if the buttons arent activated it shows “system ok” but it flickers at the end of the loop because of the delay. but if i dont have the delay the loop moves so fast the words arent visable. and i read somewhere online that the goto statements arent good coding when using arduino.

You don't seem to understand that you only write to an LCD -once- when a condition has changed.
Not initialise the LCD and write to it over and over with every loop.
That causes the flicker.

Show us your latest coding attempt.
Leo..

im not having a problem with covering up the words if the buttons arent activated

I know. I was heading up a problem you will have when you make the next step. However you seem to be resisting taking that step with all your might.

Wawa:
I see ‘flickering’ as re-writing the same text over and over with each loop.
With a state machine, new text is written once when a button is pressed or released.
Not with every loop().

On hd44780 displays there typically is no flickering when writing the exact same thing over and over again to the exact same location.
What does cause flickering is the changing of pixels since the LiquidCrystal used in these low cost displays is
very slow to change pixel states.
The key to avoiding flicker is to avoid needlessly changing pixels by doing things like clearing the display before updating the display, particularly if nothing has changed.

— bill