const int trigPin = 9;
const int echoPin = 10;
int buzzpin=7;
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzpin, 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;
Serial.println(distance);
delay(100);
if(distance <= 40 && distance >30)
{
digitalWrite(buzzpin, HIGH);
delay(2000);
digitalWrite(buzzpin, LOW);
delay(2000);
}
if(distance <= 30 && distance>20)
{
digitalWrite(buzzpin, HIGH);
delay(500);
digitalWrite(buzzpin, LOW);
delay(500);
}
if(distance <= 20 && distance> 10)
{
digitalWrite(buzzpin, HIGH);
delay(50);
digitalWrite(buzzpin, LOW);
delay(50);
}
if(distance <= 10)
{
digitalWrite(buzzpin, HIGH);
}
if(distance>40)
{
digitalWrite(buzzpin, LOW);
}
}
Comments