//Import i2C lCD library
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
const int trigPin = 5;
const int echoPin = 18;
long duration;
float distanceCm;
float distanceInch;
const int buzzer = 25;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup() {
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT); // Sets buzzer as an Output
}
void loop() {
// 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);
// Calculate the distance
distanceCm = duration * SOUND_SPEED / 2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
if(distanceCm < 100)
tone(buzzer, 262, 250);
else tone(buzzer, 0, 0);
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("Distance");
// set cursor to first column, second row
lcd.setCursor(0, 1);
lcd.print(distanceCm);
lcd.print(" cm");
delay(1000);
lcd.clear();
}