#include<LiquidCrystal_I2C.h>
#define echoPin 2
#define trigPin 3
#define buzz 13
#define buzzer 12
long distance;
int duration;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(echoPin,INPUT);
pinMode(trigPin,OUTPUT);
pinMode(buzz,OUTPUT);
lcd.begin();
lcd.backlight();
//lcd.print("done");
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH); //use to send the waves
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distance = duration*(0.034/2);
if(distance > 100){
digitalWrite(buzzer,HIGH);
tone(buzzer,1500);
}
else{
digitalWrite(buzzer,LOW);
}
digitalWrite(buzz,HIGH);
Serial.print("THE DISTANCE IS ");
Serial.println(distance);
Serial.print("CM");
lcd.print("Distance is =");
lcd.setCursor(0,1);
lcd.print(distance);
//lcd.clear();
delay(1000);
lcd.clear();
}
Comments
Please log in or sign up to comment.