Skip to content

Instantly share code, notes, and snippets.

@bcdejp
Created December 15, 2014 07:27
Show Gist options
  • Save bcdejp/1fc24d33b8fd997aea99 to your computer and use it in GitHub Desktop.
Save bcdejp/1fc24d33b8fd997aea99 to your computer and use it in GitHub Desktop.
超音波距離センサモジュールHC-SR04を制御する
#!/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