// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define echoPin 7 //connect echo pin of ultrasonic sensor
#define trigPin 6 //connect trigger pin of ultrasonic sensor
#define buzz 9
long duration; // declare variables to hold duration and distance
int distance;
//the setup function runs once when you place reset or power the bord
void setup() {
pinMode(trigPin,OUTPUT); //set trigPin as output pin of Arduino
pinMode(echoPin,INPUT); //set echoPin as output pin of Arduino
pinMode(buzz, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
}
//the loop function runs over and over again forever
void loop() {
digitalWrite(trigPin,LOW); //generate square wave at trigger pin
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);//calculation of distance of obstacle
distance=(duration*0.0343/2);
//if distance less than 20cm and more than 0 (0 or less means over);
if (distance <+20 && distance >=0){
//buzz
digitalWrite(buzz, HIGH);
}else{
//don't buzz
digitalWrite(buzz, LOW);
}
lcd.setCursor(0,0);
lcd.print("Distance : "); //print distance on the LCD
lcd.print(distance);
lcd.println(" cm ");
delay(1000);
}