-
Notifications
You must be signed in to change notification settings - Fork 0
/
code
39 lines (39 loc) · 849 Bytes
/
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#define trigPin 13
#define echoPin 12
#define buzzer 11
#define buzzer2 10
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(buzzer2, OUTPUT);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
// Added this line delayMicroseconds(2);
// Added this line digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line delayMicroseconds(10);
// Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 90) {
Page | 8
digitalWrite(buzzer,HIGH);
digitalWrite(buzzer2,LOW);
} else
{ digitalWrite(buzzer,LOW);
digitalWrite(buzzer2,HIGH);
}
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
} else {
Serial.print(distance); Serial.println(" cm");
}
delay(500);
}