-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_sensor_with_interrupt.py
57 lines (47 loc) · 1.26 KB
/
hello_sensor_with_interrupt.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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
print "Distance measurement in progress"
GPIO.setwarnings(False)
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pulse_start = time.time()
pulse_end = time.time()
waiting_for_high = True
waiting_for_low = False
def send_trigger():
time.sleep(1)
GPIO.output(TRIG,True)
time.sleep(0.00001)
GPIO.output(TRIG,False)
def on_echo_pulse(channel):
global pulse_end, pulse_start, got_high, waiting_for_high, waiting_for_low
if GPIO.input(ECHO) == GPIO.LOW and waiting_for_low:
#print 'echo pulse low'
pulse_end = time.time()
print_distance(pulse_end,pulse_start)
waiting_for_low = False
waiting_for_high = True
elif waiting_for_high:
#print 'echo pulse high'
pulse_start = time.time()
waiting_for_high = False
waiting_for_low = True
def print_distance(pend,pstart):
pulse_duration = pend - pstart
distance = pulse_duration * 17150
distance = round(distance,2)
print 'Distance %s cm' % distance
send_trigger()
try:
GPIO.add_event_detect(ECHO,GPIO.BOTH, callback=on_echo_pulse)
GPIO.output(TRIG,False)
print "Waiting for Sendor to settle"
time.sleep(2)
send_trigger()
print 'press any key to terminate'
raw_input()
finally:
GPIO.cleanup()