#include <LiquidCrystal.h>
// Initialize the library with the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//defines pins numbers
const int trigPin = 9;
const int echoPing = 10;
//defines variables
long duration;
int distance;
void setup() {
// Set up the number of columns and rows for the LCD
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT); //Sets the trigPin as an Output
pinMode(echoPing, INPUT); //Sets the echoPin as an Input
Serial.begin(9600); //Start the serial communication
}
void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
//Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPing, HIGH);
//Calculating the distance
distance = duration*0.034/2;
//Display the distance on the LCD
lcd.clear();
lcd.setCursor(0,0); //Start printing at the first row
lcd.print("Distance:");
lcd.print(distance);
delay(500);
}