#include<LiquidCrystal_I2C.h>
#include<Wire.h>
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
//initialize the liquid crystal library
//the first parameter is the I2C address
//the second parameter is how many rows are on your screen
//the third parameter is how many columns are on your screen
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
//initialize lcd screen
lcd.init();
// turn on the backlight
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
//wait for a second
delay(1000);
lcd.setCursor(0,0);
lcd.print("Dist: ");
lcd.println(distance);
if (distance <= 100){
lcd.print("");
lcd.setCursor(0,1);
lcd.print("Object near!!!!!");
}
else{
lcd.print("");
lcd.setCursor(0,1);
lcd.print("All clear.......");
}
}