#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int trigPin = 8;
int echoPin = 9;
int LED = 4;
int buzzer =3;
//define variable
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//int LED = 3;
//int buzzer =4;
pinMode(LED, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//Set the trigPin on HIGH 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
distance = duration*0.034/2;
if (distance <=20 && distance >=10)
{
// display warning
// led on
//
lcd.setCursor(1,0); //Set cursor to character 2 on line 0
lcd.print(" ");
lcd.print("Warning");
digitalWrite(LED, HIGH);
}
if (distance <10)
{
// display warning
// led blinking
// buzzer sound..
// digitalWrite(led1, HIGH);
lcd.print(" ");
lcd.print("Warning: too close");
digitalWrite(LED, HIGH);
tone(buzzer, 1000);
delay (1000);
noTone(buzzer);
delay (1000);
}
}