-
Notifications
You must be signed in to change notification settings - Fork 75
/
ranger.py
35 lines (28 loc) · 798 Bytes
/
ranger.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
import RPi.GPIO as GPIO
import time
trigger_pin = 18
echo_pin = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)
def send_trigger_pulse():
GPIO.output(trigger_pin, True)
time.sleep(0.0001)
GPIO.output(trigger_pin, False)
def wait_for_echo(value, timeout):
count = timeout
while GPIO.input(echo_pin) != value and count > 0:
count = count - 1
def get_distance():
send_trigger_pulse()
wait_for_echo(True, 10000)
start = time.time()
wait_for_echo(False, 10000)
finish = time.time()
pulse_len = finish - start
distance_cm = pulse_len / 0.000058
distance_in = distance_cm / 2.5
return (distance_cm, distance_in)
while True:
print("cm=%f\tinches=%f" % get_distance())
time.sleep(1)