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);
}