-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsonar_sensor.py
More file actions
48 lines (42 loc) · 1.23 KB
/
sonar_sensor.py
File metadata and controls
48 lines (42 loc) · 1.23 KB
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
#!/usr/bin/env python
import RPi.GPIO as gpio
import time
import sys
import signal
def signal_handler(signal, frame): # ctrl + c -> exit program
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
gpio.setmode(gpio.BCM)
trig = 27 # 7th
echo = 17 # 6th
gpio.setup(trig, gpio.OUT)
gpio.setup(echo, gpio.IN)
time.sleep(0.5)
print ('-----------------------------------------------------------------sonar start')
try :
while True :
gpio.output(trig, False)
time.sleep(0.1)
gpio.output(trig, True)
time.sleep(0.00001)
gpio.output(trig, False)
while gpio.input(echo) == 0 :
pulse_start = time.time()
while gpio.input(echo) == 1 :
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17000
if pulse_duration >=0.01746:
print('time out')
continue
elif distance > 300 or distance==0:
print('out of range')
continue
distance = round(distance, 3)
print ('Distance : %f cm'%distance)
except (KeyboardInterrupt, SystemExit):
gpio.cleanup()
sys.exit(0)
except:
gpio.cleanup()