-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic.py
59 lines (47 loc) · 1.4 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
55
56
57
58
59
import RPi.GPIO as GPIO
import time
def ultrasonic():
# Set GPIO pin numbers
GPIO_TRIG = 19
GPIO_ECHO = 4
def setup():
# Use BCM GPIO references
GPIO.setmode(GPIO.BCM)
# Set pins as output and input
GPIO.setup(GPIO_TRIG, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
# Set trigger pin to low
GPIO.output(GPIO_TRIG, False)
time.sleep(0.5)
def object_detected(distance):
if distance < 15:
return True
else:
return False
def get_distance():
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIG, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIG, False)
# Measure time between start and end of pulse
start_time = time.time()
end_time = time.time()
while GPIO.input(GPIO_ECHO) == 0:
start_time = time.time()
while GPIO.input(GPIO_ECHO) == 1:
end_time = time.time()
# Calculate distance based on time
duration = end_time - start_time
distance = duration * 17150 # Speed of sound is 34300 cm/s, halved for return trip
distance = round(distance, 2)
return distance
def cleanup():
GPIO.cleanup()
setup()
distance = get_distance()
if object_detected(distance):
print(distance)
return False
time.sleep(1)
while True:
ultrasonic()