import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BOARD)
trig = 15
echo = 16
led1 = 11
led2 = 12
gpio.setup(echo, gpio.IN)
gpio.setup(led1, gpio.OUT)
gpio.setup(led2, gpio.OUT)
try :
while True :
gpio.output(trig,False)
time.sleep(0.5)
gpio.output(trig, True)
time.sleep(0.000001)
gpio.output(trig, False)
while True :
if gpio.input(echo) == 0 :
pass
else :
start_time = time.time()
break
while True :
if gpio.input(echo) == 1 :
pass
else :
end_time = time.time()
break
distance = (end_time - start_time) * 17000
print ' Dist = ', distance
if distance < 30 :
gpio.output(led1, gpio.HIGH)
# elif distance < 80 :
# gpio.output(led2, gpio.HIGH)
else :
gpio.output(led2, gpio.HIGH)
time.sleep(1)
gpio.output(led1, gpio.LOW)
gpio.output(led2, gpio.LOW)
except KeyboardInterrupt :
gpio.cleanup()
print ' '
print ' == Good Bye == '
브렛보드 두개를 이용했다. 1곳에 해도 당근 된다.
모니터에 거리를 표시해 본 것이다.