LCD 1604 Flickering

How to stop lcd from flickering. Someone help me on this. I am using "lcd.clear(); and cannot avoid the same. All the code runs fine and everything is functional.

Thanks

So I have explained in your other thread, how to post your code. Just a refresher:

First read the instructions on how to post questions.

Then post your code using the "copy code for forum" option in the IDE after you have used "Auto Format" - control-T.

Include also a diagram of how you are connecting your parts.

Include a web reference to each of the parts in your project except for the Arduino; just specify which Arduino.

And explain what you expect your code to do.


Now the answer.

Do not use "lcd.clear();"

It is probably not necessary in setup() and almost never required in the loop().

Plan what you need to display, print only whatever part needs to change using setCursor, and only change it when the new data is different from the old and no more often than about four times per second.

The usual cause of flickering is continuously re-writing the display in the loop, and using "lcd.clear();" will completely foul it up.

Thank you Paul

The problem is when the characters and locations change according to the inputs the previous has to be cleared if not you get a mixed text.

Here is the code -

#include <LiquidCrystal.h>
int sump=A0;
int qut=A1;
int hlf=A2;
int thf=A3;
int ful=A4;
int motor=8;
int buz=7;
int s;
int q;
int h;
int t;
int f;
int i;     //motor status flag
int v=100; //comparison variable(needs some adjustment)
int b=0;   //buzzer flag
int m=0;   //motor flag
int c=0;   //sump flag

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{

pinMode(qut,INPUT);
pinMode(hlf,INPUT);
pinMode(qut,INPUT);
pinMode(ful,INPUT);
pinMode(sump,INPUT);
pinMode(motor,OUTPUT);
pinMode(buz,OUTPUT);
lcd.begin(16, 2);
digitalWrite(buz,LOW);
}

void loop()
{

i=digitalRead(motor);
s=analogRead(sump);
q=analogRead(qut);
h=analogRead(hlf);
t=analogRead(thf);
f=analogRead(ful);
lcd.clear();

if(f>v && t>v && h>v && q>v )
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("FULL");
m=0;
b=0;



}
else
{
if(f<v && t>v && h>v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("3/4th");
b=0;
}
else
{
if(f<v && t<v && h>v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("HALF");
m=1;
b=0;
}
else
if(f<v && t<v && h<v && q>v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("1/4th");
b=0;
}
else
{
if(f<v && t<v && h<v && q<v)
{
lcd.setCursor(0,0);
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.print("_");
lcd.setCursor(5,0);
lcd.print("LOW");
b=0;
}
else

{
digitalWrite(motor,LOW);
lcd.setCursor(0,0);
lcd.print("ERROR!");
b=1;
}
}}}
if(i==HIGH)
{
lcd.setCursor(0,1);
lcd.print("Motor ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("Motor OFF");
}



if(s>v && m==1)
{
digitalWrite(motor,HIGH);
}
if(s<v)
{
digitalWrite(motor,LOW);
lcd.setCursor(11,0);
lcd.print("Low");
lcd.setCursor(11,1);
lcd.print("Sump");
c=1;
}
if(s>v)
{
c=0;
}

if(m==0)
{
digitalWrite(motor,LOW);
}

if(b==1 || c==1)
{
digitalWrite(buz,HIGH);
delay(500);
digitalWrite(buz,LOW);
}
else
{
digitalWrite(buz,LOW);
}
delay(100);
lcd.clear();
}

OK, there's the problem all right - you have lcd.clear(); in the loop - get rid of it!

Because you failed to use "Auto Format" - control-T, your code is extremely difficult to read.

You are actually using lcd.setCursor(x,y), so you do know how to do this! All you have to do is to write the same length of text at each point and it will simply over-write the whole of the previous one and leave no junk. But it looks like you really did know this already, but just did not get it quite right.

Then you put a delay(200) in the loop (except you do not need to do this if you are doing the buzzer thing) so that it happens no faster than five times per second. If you need to do other things more often, there are alternate approaches to writing the code.

The problem is when the characters and locations change according to the inputs the previous has to be cleared if not you get a mixed text.

More specifically:

  • when you display a short word such as HELLO and then replace it with a long word such as GOODBYE everything is ok.
  • when you reverse the sequence and replace GOODBYE with HELLO you get HELLOYE since only the first five characters of GOODBYE are replaced with HELLO. The last two characters YE remain displayed.

Paul's suggestion is to make all of your messages the same length. That is, pad your shorter words or values with spaces where necessary.

Another technique (but not necessarily the best) is to overwrite your entire first word with spaces before displaying the second word.

In other words:

  • set the cursor and display your first word
  • set the cursor and display enough blanks to 'erase' your first word
  • set the cursor and display your second word

This actually takes less processor time than clearing the screen and will eliminate your flickering.

Don

My apologies for not using "Auto Format", and I thank both of you.

Using spaces and removing lcd.clear() solved my problem. But wonder why lcd.clear is used?

How do I Auto Format?

Edit #2 to show formatted code. And add a note to say what you have done.

Yes, ctrl-T is the best feature of the Arduino IDE.
It solves most logical problems.

When you look at random "code" on the internet you will bw amazed by some of the statements.
I believe in printing stuff out on paper occasionally. You can tidy up the logic.
You just ask yourself: "Is lcd.clear() needed here?"

David.

Thanks a lot David

sydhalls:
Using spaces and removing lcd.clear() solved my problem. But wonder why lcd.clear is used?

lcd.clear() is used when you want to clear the entire display.

But - two buts - it is a very slow command as apparently the hardware has some internal function that laboriously sets each display RAM byte to 0x20, one at a time (and all of RAM, not just what is currently on display) so is the slowest amongst the commands provided and - this is where you made the mistake - you do not want to clear the whole display after setup().

Sometimes you might want to clear the whole display and write something entirely different, but in the vast majority of cases - as here - you just want to update indicators as particular elements.

"Auto Format" is under "Tools" in the IDE but Control-T is a shorthand. It can be annoying sometimes as it spaces out things you want to keep together but it helps you to keep units of code together. Use spacing lines only to separate major sections of code.

Just curious. When you look at your program a year or two from now, without running the program and looking at the display, are you going to be able to tell what the 'magic number' 219 represents and how you came up with that particular value?

Don

Sorry - several decades of teaching this stuff is resurfacing, even after being retired for a few more decades.

My problem is solved by using spaces, still I wonder why commands like lcd.clear() & delay() are used or not re-written?. The number "219" was chosen to represent levels, "a square". I still have certain programming issues and have posted the same under programming questions titled "Water Level Controller". (now at pg.5). Though I respect & value all of you guys who had taken time to post replies to my queries, I am still disappointed because I haven't got my queries answered. :frowning:

sydhalls:
My problem is solved by using spaces, still I wonder why commands like lcd.clear() & delay() are used or not re-written?. The number "219" was chosen to represent levels, "a square". I still have certain programming issues and have posted the same under programming questions titled "Water Level Controller". (now at pg.5). Though I respect & value all of you guys who had taken time to post replies to my queries, I am still disappointed because I haven't got my queries answered. :frowning:

Now you know how we feel.

Don

I just requested you guys to write me a simple code adding dry run feature, being experts in arduino programming instead got irrelevant answers.

Your statement " Now you know how we feel". Is it meant to hurt?

sydhalls:
I just requested you guys to write me a simple code adding dry run feature, being experts in arduino programming instead got irrelevant answers.

Your statement " Now you know how we feel". Is it meant to hurt?

In response to your first paragraph . . . I can't see how it applies in any way to this thread. Perhaps it applies to another one.

In response to your second one . . . It was not meant to hurt, it was intended to get you to look back at the thread to find out what I was referring to.

The question in your original post asked how to eliminate the flickering in your LCD. This problem occurs quite frequently and as a matter of fact had you used the phrase LCD flickering in the stealth search box at the upper right of the page you would have quickly found the same answer that was provided to you at the end of reply #1.

The problem you posed (not really a question) in your reply #2 was answered in reply #3 and also in reply #4.

Your question in reply #5 was answered in reply # 9.

Your question in reply #6 was answered in reply #7 and also in reply #9.

My question in reply #10 was not answered. Look at it again.

Don

Your question in 10 is answered in reply 11, go back and take a look again.

Not exactly.

In number 11 you answered "The number "219" was chosen to represent levels, "a square" - which both you and I already knew.

My question asked you to speculate on whether you, in the future, would remember not only what what the number represented but also how you arrived at the value. The answer should have been yes, no or maybe.

What I was really getting at is the value of using appropriate comments in your code.

So, in your code the first time you display a box you might write:

lcd.print(char(219));  // display a box

And somewhere else you might write:

// the HD44780 data sheet shows a suitable 'box' where column 1101 crosses row 1011.  
// The resulting binary number 0b11011011 converts to 219 in decimal.

Don