-
Notifications
You must be signed in to change notification settings - Fork 0
/
UltrasonicSensor_Arduino.c
50 lines (41 loc) · 1.02 KB
/
UltrasonicSensor_Arduino.c
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
40
41
42
43
44
45
46
47
48
49
50
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(D2, INPUT); //echo
pinMode(D1, OUTPUT); //trigger
pinMode(D3, OUTPUT); // red
pinMode(D4, OUTPUT); // yellow
pinMode(D7, OUTPUT); // green
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(D2, LOW);
delayMicroseconds(2);
digitalWrite(D1, HIGH);
delayMicroseconds(10);
digitalWrite(D1, LOW);
float duration = pulseIn(D2,HIGH);
//Serial.print("duration: ");
//Serial.println(duration);
float speed = 0.0343;
float distance = (duration * speed) / 2;
Serial.print("Distance:");
Serial.println(distance);
if(distance < 8){
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
digitalWrite(D7, LOW);
}
else if(distance >= 8 && distance <=15)
{
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
digitalWrite(D7, LOW);
}
else if(distance >= 15 && distance <= 200){
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D7, HIGH);
}
delay(1000);
}