Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- TRIG = 23
- ECHO = 24
- def init_us():
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(TRIG, GPIO.OUT)
- GPIO.setup(ECHO, GPIO.IN)
- GPIO.output(TRIG, False)
- print "Waiting For Sensor To Settle"
- time.sleep(2)
- def measure_distance():
- global pulse_end, pulse_start
- 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 * 17150
- distance = round(distance, 2)
- return num2words(distance)
- def cleanup_us():
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement