Created
December 15, 2014 07:27
-
-
Save bcdejp/1fc24d33b8fd997aea99 to your computer and use it in GitHub Desktop.
超音波距離センサモジュールHC-SR04を制御する
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def reading(sensor): | |
import time | |
import RPi.GPIO as GPIO | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BOARD) | |
TRIG = 11 | |
ECHO = 13 | |
if sensor == 0: | |
GPIO.setup(TRIG,GPIO.OUT) | |
GPIO.setup(ECHO,GPIO.IN) | |
GPIO.output(TRIG, GPIO.LOW) | |
time.sleep(0.3) | |
GPIO.output(TRIG, True) | |
time.sleep(0.00001) | |
GPIO.output(TRIG, False) | |
while GPIO.input(ECHO) == 0: | |
signaloff = time.time() | |
while GPIO.input(ECHO) == 1: | |
signalon = time.time() | |
timepassed = signalon - signaloff | |
distance = timepassed * 17000 | |
return distance | |
GPIO.cleanup() | |
else: | |
print "Incorrect usonic() function varible." | |
print reading(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment