#include <LiquidCrystal.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// defines pins numbers
const int trigPin = 8;
const int echoPin = 10;
//const int buzzer= 9;
//const int led =13;
// defines variables
long duration;
int distance;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
void setup()
{
Serial.begin(9600);
//pinMode(buzzer, OUTPUT);
pinMode (trigPin, OUTPUT); // Sets the trigpin as an Output
pinMode (echoPin, INPUT); // Sets the echopin as an Input
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
}
void loop() {
// Clears the trigin
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;
// if (distance <= 40)
// {
// digitalWrite(buzzer,HIGH);
// digitalWrite(led,HIGH);
// }
// else
//{
//digitalWrite(buzzer,LOW);
//digitalWrite(led,LOW);
//}
// Prints the distance on the Serial Monitor
Serial.print("Distance(in cm): ");
Serial.println(distance);
delay(2000);
lcd.clear();
delay(100);
lcd.setCursor(1,0); //Set cursor to character 2 on line 0
lcd.print("Distance(in cm)");
delay(500);
lcd.setCursor(2,1); //Move cursor to character 2 on line 1
lcd.print(distance);
delay(1000);
}