#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int trig=12;
int echo=11;
int buzzer=9;
int led=4;
int time;
int distance;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
// put your setup code here, to run once:
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
time=pulseIn(echo,HIGH);
Serial.print("travelling time of the wave;");
Serial.println(time);
distance=time/2*0.034;
Serial.print("Distance of the object;");
Serial.println(distance);
delay(1000);
if(distance<300)
{
tone(buzzer,1000);
digitalWrite(led, HIGH);
lcd.setCursor(2,0);
lcd.print("**Alert!**");
lcd.setCursor(0,1);
lcd.print("Someone is there");
}
else{
noTone(buzzer);
digitalWrite(led, LOW);
lcd.clear();
}
// put your main code here, to run repeatedly:
}