Created
March 12, 2016 21:04
-
-
Save pzula/a5a16e624a5abb47ead3 to your computer and use it in GitHub Desktop.
Ultrasonic sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setwarnings(False) | |
timeout = 0.020 | |
while 1: | |
GPIO.setup(11, GPIO.OUT) | |
#cleanup output | |
GPIO.output(11, 0) | |
time.sleep(0.000002) | |
#send signal | |
GPIO.output(11, 1) | |
time.sleep(0.000005) | |
GPIO.output(11, 0) | |
GPIO.setup(11, GPIO.IN) | |
goodread=True | |
watchtime=time.time() | |
while GPIO.input(11)==0 and goodread: | |
starttime=time.time() | |
if (starttime-watchtime > timeout): | |
goodread=False | |
if goodread: | |
watchtime=time.time() | |
while GPIO.input(11)==1 and goodread: | |
endtime=time.time() | |
if (endtime-watchtime > timeout): | |
goodread=False | |
if goodread: | |
duration=endtime-starttime | |
distance=duration*34000/2 | |
print distance | |
Posts: 1 | |
Joined: Thu Mar 05, 2015 5:07 am |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment