-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrasonic distance calculator
73 lines (41 loc) · 867 Bytes
/
ultrasonic distance calculator
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led_, OUTPUT);
}
void loop()
{ long duration,distance,distmeter ;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
distmeter = distance/100;
if (distance < 50)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
if (distance<10)
{
digitalWrite(led_,HIGH);
Serial.print("The object is very close!!!!!!!");
}
else{
digitalWrite(led_,LOW);
}
Serial.print("The distance of the object is: ");
Serial.print(distance);
Serial.println(" cm");
Serial.println(distmeter," meter");
delay(500);
}