#define Trigger 5
#define Echo 4
#define BuzzerPin 19
long Duration, Distance;
void setup() {
pinMode(Trigger, OUTPUT) ;
pinMode(Echo, INPUT);
pinMode(BuzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(Trigger, LOW);
delayMicroseconds(5);
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
Duration = pulseIn(Echo,HIGH);
Serial.print("Duration is : ");
Serial.print(Duration);
Serial.println(" us");
Distance = 0.034*(Duration/2);
Serial.print("Distance is : ");
Serial.print(Distance);
Serial.println(" cm");
if(Distance <= 50){
tone(BuzzerPin, 262, 100);
}
delay(250);
}