Flickering LCD

Hi, I have a sensor returning it’s result to an LCD display with a small speaker driven off my Mega. As the level increases a Bar-graph is produced on a 16x2 and the tone of the speaker rises.

It all works but the LCD flickers and changes with the sensor return (I’m using a 10k pot to simulate at the moment).

If I remove the ‘clear LCD’ command the display does not flicker, but the the text from line 1 (or is it 0?) “sensor level” scrolls into line 2 as the bar-graph drops.

I’m very new to this so am a bit stuck with what is probably a simple error I’ve made :blush:

This is my code…

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int LCDval;

void setup(){ 
  lcd.begin(16, 2);
   Serial.begin(9600);
}

void loop()
{
  lcd.clear();
  lcd.print("Sensor Level");
  int value = analogRead(15);
  lcd.setCursor(15,0);
  //lcd.print(value);//
  lcd.setCursor(0,1);
  LCDval = value / 51.15;
  for(int i = 0; i < LCDval; i++) {
    lcd.write(255);
  }
  delay(1);

// read the sensor:
  int sensorReading = analogRead(15);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below
  // depending on the range your sensor's giving:
  int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

  // play the pitch:
  tone(40, thisPitch, 10);
  delay(5);        // delay in between reads for stability
}

The output from analogRead() ranges from 0 to 1023. Dividing that value by 51.15 results in a value between 0 and 20.

How are you displaying 20 characters on one row of a 16x2 LCD?

PaulS: The output from analogRead() ranges from 0 to 1023. Dividing that value by 51.15 results in a value between 0 and 20.

How are you displaying 20 characters on one row of a 16x2 LCD?

Good point and cheers! XD

This is what comes from making 'Frankencode' from combining tutorials and online bits! :(

I'll edit that bit, but I'm still stuck with the flickering (cool that there is such an active forum 8) )

One way to combat clearing an LCD would be to keep track of the last value printed and compare it to the current value. If the values are different, clear the LCD and print the new value.

Arrch: One way to combat clearing an LCD would be to keep track of the last value printed and compare it to the current value. If the values are different, clear the LCD and print the new value.

Unless I'm misunderstanding the issue I have is that the top line of text gets repeated on the lower line (the bar graph) it appears as the value drops, effectively scrolling in from the right.

I need to keep the top line of text separate from the bar graph

AndrewD: Unless I'm misunderstanding the issue I have is that the top line of text gets repeated on the lower line (the bar graph) it appears as the value drops, effectively scrolling in from the right.

I need to keep the top line of text separate from the bar graph

Before you print the "sensor line", you need to set the cursor to (0,0).

It sounds like the top line is static, so I would probably just print it in setup and update the bar graph in loop (without using clear).

Arrch:

AndrewD: Unless I'm misunderstanding the issue I have is that the top line of text gets repeated on the lower line (the bar graph) it appears as the value drops, effectively scrolling in from the right.

I need to keep the top line of text separate from the bar graph

Before you print the "sensor line", you need to set the cursor to (0,0).

It sounds like the top line is static, so I would probably just print it in setup and update the bar graph in loop (without using clear).

Tried to add that but it killed the display (just got the top line as a full signal bar graph)

I think you right about the top line needing to be static for this, but it might change later when I add an RFID access.

I'm already learning I need to add lots of notes in my code!! :fearful:

AndrewD: Hi, I have a sensor returning it's result to an LCD display with a small speaker driven off my Mega. As the level increases a Bar-graph is produced on a 16x2 and the tone of the speaker rises.

It all works but the LCD flickers and changes with the sensor return (I'm using a 10k pot to simulate at the moment).

Two things with the LCD:

(1) Slow down the re-write rate. If you refresh the LCD too fast, you start to alias with the LCD's internal timing and can get all kinds of flickering and slowly scrolling bars like 1970's TV sets had! Try for about 5 to 10 updates per second.

(2) You don't need to clear the LCD each time around the loop. The lcd.clear() command takes quite a bit of time to run compared to other lcd commands. The result is that your display is alternating between "display" and "blank" at a rapid rate, causing more flicker.

Instead, write each string to fill the entire LCD line.

Imagine you first displayed this:

"Hello there"

Then, without clearing, tried to display "12345". You would get:

"12345 there"

So, instead display your text with enough trailing spaces to "erase" the rest of the LCD line, like this:

"Hello there "

Then:

"12345 "

The extra spaces after the "12345" will erase the leftover "there" from the previous line.

Of course, you need to reset your cursor position each time... I assumed you know that... :)

AndrewD: Tried to add that but it killed the display (just got the top line as a full signal bar graph)

Can't see the code, can't tell you how to fix it.

Arrch:

AndrewD: Tried to add that but it killed the display (just got the top line as a full signal bar graph)

Can't see the code, can't tell you how to fix it.

I put the code in my first post :D

Krupski:

AndrewD: Hi, I have a sensor returning it's result to an LCD display with a small speaker driven off my Mega. As the level increases a Bar-graph is produced on a 16x2 and the tone of the speaker rises.

It all works but the LCD flickers and changes with the sensor return (I'm using a 10k pot to simulate at the moment).

Two things with the LCD:

(1) Slow down the re-write rate. If you refresh the LCD too fast, you start to alias with the LCD's internal timing and can get all kinds of flickering and slowly scrolling bars like 1970's TV sets had! Try for about 5 to 10 updates per second.

(2) You don't need to clear the LCD each time around the loop. The lcd.clear() command takes quite a bit of time to run compared to other lcd commands. The result is that your display is alternating between "display" and "blank" at a rapid rate, causing more flicker.

Instead, write each string to fill the entire LCD line.

Imagine you first displayed this:

"Hello there"

Then, without clearing, tried to display "12345". You would get:

"12345 there"

So, instead display your text with enough trailing spaces to "erase" the rest of the LCD line, like this:

"Hello there "

Then:

"12345 "

The extra spaces after the "12345" will erase the leftover "there" from the previous line.

Of course, you need to reset your cursor position each time... I assumed you know that... :)

Cheers, that sounds like what I need to be doing, though it's all still double Dutch to me! :blush:

AndrewD: I put the code in my first post :D

Really? Because I don't see anything in your code about setting the cursor back to 0,0 ...

Arrch:

AndrewD: I put the code in my first post :D

Really? Because I don't see anything in your code about setting the cursor back to 0,0 ...

Okay so your message said

Can't see the code, can't tell you how to fix it.

but I had put it in my first post.

Now you can see the code, but not the cursor position....I'm confused?? :roll_eyes:

It sounds like if some kind soul can give me the code and where to put it, the problem is sorted! 8)

AndrewD:
I put the code in my first post :smiley:

People have suggested various fixes to you. Have you tried them? If so, your code no longer matches the original. You need to post your current code if you expect people to help resolve the problems in it.

PeterH:

AndrewD: I put the code in my first post :D

People have suggested various fixes to you. Have you tried them? If so, your code no longer matches the original. You need to post your [u]current[/u] code if you expect people to help resolve the problems in it.

Yes, I tried the cursor (0,0) and it killed the display (just a full bar on line 1) so the code reverted to the functional but flickering.

The suggestion to put "Hello world " looks good, but I'm not sure how that will help as it's a bar graph on the second line (does it start line 0 or 1?).

I think I need a way to stop the LCD from taking information intended purely for the first lane and repeating it on the second.

I will just end up posting lots of code that does not work.

I'm grateful for the time and suggestions, don't want anyone thinking I'm not! :blush:

AndrewD: Now you can see the code, but not the cursor position....I'm confused?? :roll_eyes:

Let me ammend my previous statement:

Can't see any code that includes the changes I recommended that you say you tried without success, so I can't help.

AndrewD: Yes, I tried the cursor (0,0) and it killed the display (just a full bar on line 1) so the code reverted to the functional but flickering.

The suggestion to put "Hello world " looks good, but I'm not sure how that will help as it's a bar graph on the second line (does it start line 0 or 1?).

I think I need a way to stop the LCD from taking information intended purely for the first lane and repeating it on the second.

I will just end up posting lots of code that does not work.

I'm grateful for the time and suggestions, don't want anyone thinking I'm not! :blush:

If you don't want to post updated code, don't expect updated help.

Arrch:

AndrewD:
Now you can see the code, but not the cursor position…I’m confused?? :roll_eyes:

Let me ammend my previous statement:

Can’t see any code that includes the changes I recommended that you say you tried without success, so I can’t help.

AndrewD:
Yes, I tried the cursor (0,0) and it killed the display (just a full bar on line 1) so the code reverted to the functional but flickering.

The suggestion to put "Hello world " looks good, but I’m not sure how that will help as it’s a bar graph on the second line (does it start line 0 or 1?).

I think I need a way to stop the LCD from taking information intended purely for the first lane and repeating it on the second.

I will just end up posting lots of code that does not work.

I’m grateful for the time and suggestions, don’t want anyone thinking I’m not! :blush:

If you don’t want to post updated code, don’t expect updated help.

Fair enough, I’m going to try and sort the first line as static and I’ll post the code. The Arduino not wanting to accept the code is not helping right now, I have to disconnect and close the Arduino terminal, I’m going to pull the Genuine Mega off my 3D printer and use that.

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int LCDval;

void setup(){
lcd.begin(16, 2);
Serial.begin(9600);

}

void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Sensor Level”);
int value = analogRead(15);
lcd.setCursor(15,0);
//prints value of sensor on line 1
//lcd.print(value);
lcd.setCursor(0,2);
LCDval = value / 63.93;
for(int i = 0; i < LCDval; i++) {
lcd.write(255);
}
delay(4);

// read the sensor:
int sensorReading = analogRead(15);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below
// depending on the range your sensor’s giving:
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

// play the pitch:
tone(40, thisPitch, 10);
delay(5); // delay in between reads for stability
}

Code as it stands, I’ve moved the cursor (and a load of other things that failed), but frankly it’s all a bit of a stab in the dark!

Do you have a a line of code to put the fixed line “Sensor value” in the the setup? I’ve tried to do stuff there and it refuses to compile (I’m sure it’s quietly sniggering calling me names too!).

Once I’ve got the hang of it, I’m sure I’ll be sorted.

Move this into setup after lcd.begin:

  lcd.setCursor(0,0);
  lcd.print("Sensor Level");

Your lcd has two rows: 0 and 1. That would make this a problem:

  lcd.setCursor(0,2);

wildbill:
Move this into setup after lcd.begin:

  lcd.setCursor(0,0);

lcd.print(“Sensor Level”);




Your lcd has two rows: 0 and 1. That would make this a problem:


lcd.setCursor(0,2);

Thanks, sorted that out, I know it’s stable if I // out the lcd.clear(); but then the bar graph reads the increase and never drops back as it never refreshes.

Am I correct it’s picking up the sample rate of the sensor?

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int LCDval;

void setup(){
lcd.begin(16, 2);
Serial.begin(9600);

}

void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Sensor Level”);
int value = analogRead(15);
lcd.setCursor(15,0);
//prints value of sensor on line 1
//lcd.print(value);
lcd.setCursor(0,1);
LCDval = value / 63.93;
for(int i = 0; i < LCDval; i++) {
lcd.write(255);
}
delay(4);

// read the sensor:
int sensorReading = analogRead(15);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below
// depending on the range your sensor’s giving:
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

// play the pitch:
tone(40, thisPitch, 10);
delay(5); // delay in between reads for stability
}