-
Notifications
You must be signed in to change notification settings - Fork 1
/
distance.py
85 lines (65 loc) · 2.12 KB
/
distance.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#Libraries
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#set GPIO Pins
GPIO_TRIGGER = 7
GPIO_ECHO = 11
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
#set GPIO Pins
GPIO_TRIGGER1 = 13
GPIO_ECHO1 = 15
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
GPIO.setup(GPIO_ECHO1, GPIO.IN)
def distance(trig,echo):
# set Trigger to HIGH
GPIO.output(trig, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(trig, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(echo) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(echo) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
def checkDistance(GPIO_TRIGGER, GPIO_ECHO, GPIO_TRIGGER1, GPIO_ECHO1):
d = distance(GPIO_TRIGGER, GPIO_ECHO)
d1 = distance(GPIO_TRIGGER1, GPIO_ECHO1)
print ("Measured Distance = %.1f cm" % d)
print ("Measured Distance 1 = %.1f cm" % d1)
#print ("Measured Distance 1 = %.1f cm" % dist1)
if 26.0 < d < 33.0 and 23.0 < d1 < 29.0:
print("in range")
return True
else:
print("OUT OF RANGE")
return False
if __name__ == '__main__':
try:
while True:
d = distance(GPIO_TRIGGER, GPIO_ECHO)
d1 = distance(GPIO_TRIGGER1, GPIO_ECHO1)
print ("Measured Distance = %.1f cm" % d)
print ("Measured Distance 1 = %.1f cm" % d1)
#print ("Measured Distance 1 = %.1f cm" % dist1)
if 31.0 < d < 41.0 and 23.0 < d1 < 34.0:
print("in range")
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()