// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define echoPin 7 //connect echo pin of ultrasonic sensor
#define trigPin 6 //connect trigger pin of ultrasonic sensor
long duration; // declare variables to hold duration and distance
int distance;
void setup() {
pinMode(trigPin,OUTPUT); //set trigPin as output pin of Arduino
pinMode(echoPin,INPUT); //set echoPin as output pin of Arduino
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
digitalWrite(trigPin,LOW); //generate square wave at trigger pin
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);//calculation of distance of obstacle
distance=(duration*0.0343/2);
lcd.setCursor(0,0);
lcd.print("Distance : "); //print distance on the LCD
lcd.print(distance);
lcd.println(" cm ");
delay(1000);
}