-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultrasonic.py
54 lines (35 loc) · 1.22 KB
/
ultrasonic.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
import RPi.GPIO as GPIO
import time
class Ultrasonic():
def __init__(self):
GPIO.setmode(GPIO.BCM)
self.GPIO_TRIGGER = 23
self.GPIO_ECHO = 24
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
def distance(self):
GPIO.output(self.GPIO_TRIGGER,True)
time.sleep(0.0001)
GPIO.output(self.GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(self.GPIO_ECHO) == 0:
StartTime = time.time()
while GPIO.input(self.GPIO_ECHO) == 1:
StopTime = time.time()
TimeElapsed = StopTime - StartTime
distance = (TimeElapsed*34300)/2
return distance
def cleanup(self):
GPIO.cleanup()
if __name__ == '__main__':
try:
while True:
ultrasonic = Ultrasonic()
dist = ultrasonic.distance()
print("Measured distance = %.1f cm" %dist)
time.sleep(1)
except KeyboardInterrupt:
print('Measurement stopped by User')
GPIO.cleanup()