-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
62 lines (47 loc) · 1.03 KB
/
sensors.py
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
import RPi.GPIO as io
import time
io.setmode(io.BCM)
io.setwarnings(False)
# Trigger pins
t1 = 22
t2 = 23
t3 = 24
t4 = 25
t5 = 26
# Echo pins
e1 = 16
e2 = 17
e3 = 18
e4 = 19
e5 = 20
all_triggers = [t1, t2, t3, t4, t5]
all_echoes = [e1, e2, e3, e4, e5]
io.setup(all_triggers,io.OUT)
io.setup(all_echoes,io.IN)
def get_distance(trigger, echo):
# Activate trigger.
io.output(trigger, 1)
time.sleep(0.00001)
io.output(trigger, 0)
start_time = time.time()
stop_time = time.time()
# Save start time.
while io.input(echo) == 0:
start_time = time.time()
# Save arrival time.
while io.input(echo) == 1:
stop_time = time.time()
# Convert time to centimeters.
distance = ((stop_time - start_time) * 34300) / 2
return round(distance, 2)
io.cleanup()
def dist_1():
return get_distance(t1, e1)
def dist_2():
return get_distance(t2, e2)
def dist_3():
return get_distance(t3, e3)
def dist_4():
return get_distance(t4, e4)
def dist_5():
return get_distance(t5, e5)