const int trigpin=13;
const int echopin=12;
int Led=2;
int buzzer =4;
long duration;
int distance;
void setup()
{
pinMode(trigpin,OUTPUT);
pinMode(echopin,INPUT);
pinMode(Led,OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration =pulseIn(echopin,HIGH);
distance =duration *0.034/2; //0.034 is sound velocity
Serial.print("Distance");
Serial.println(distance);
if(distance<=100)
{
digitalWrite( Led,1);
tone (buzzer,900);
delay(500);
tone (buzzer,800);
delay(500);
}
else
{
digitalWrite( Led,0);
noTone(buzzer);
}
}