Mega, LCD Keypad Shield and HC-SR04 flickering screen

Hi Guys
I stole this code to demonstrate how a LCD Keypad Shield works and added a HC-SR04 and some simple code to display distance. the output is flickering, Im a super newb please help. Out put is correct but flickers

//Sample using LiquidCrystal library
#include <LiquidCrystal.h>

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// defines pins numbers
const int trigPin = 22;
const int echoPin = 24;
// defines variables
long duration;
int distance;

// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1500) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 500) return btnLEFT;
if (adc_key_in < 700) return btnSELECT;
return btnNONE; // when all others fail, return this...
}

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

//lcd.begin(16, 2); // start the library
// lcd.setCursor(0,0);
//lcd.println(distance); // print a simple message
}

void loop()
{
//lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis()/1000); // display seconds elapsed since power-up
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
delayMicroseconds(100);

lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);

lcd.print(distance);
delayMicroseconds(100);
}

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly.

You should not have lcd.begin() in loop(). It's also far better to only update the LCD when there is a change in the distance, not every time that you go through loop().

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

It is generally not a good idea to re-initialize the library every time through loop(). Move the .begin() to setup() and use 'lcd.clear();' to clear the display.

Note: The maximum range of the HC-SR04 is about 5 meters and the round-trip-time for 6 meters is about 30000 microseconds so the default 1-second timeout on the pulseIn() is overkill. Add a third argument to set the timeout to 30000:
duration = pulseIn(echoPin, HIGH, 30000);
That will make your system more responsive when no echo is received (nothing in range).

@johnwasser excellent exactly what I needed thank you, I'm away again