Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import sys
- import time
- import RPi.GPIO as GPIO
- GPIOTrigger = 23
- GPIOEcho = 24
- def wait(sec):
- while sec > 0:
- sys.stdout.write(str(sec) + ' r')
- sec -= 1
- time.sleep(1)
- def MeasureDistance():
- GPIO.output(GPIOTrigger, True)
- time.sleep(0.00001)
- GPIO.output(GPIOTrigger, False)
- StartTime = time.time()
- while GPIO.input(GPIOEcho) == 0:
- StartTime = time.time()
- while GPIO.input(GPIOEcho) == 1:
- StopTime = time.time()
- TimeElapsed = StopTime - StartTime
- Distance = (TimeElapsed * 34300) / 2
- return Distance
- def main():
- STATUS = 0
- try:
- while True:
- Distance = MeasureDistance()
- if Distance > 10.0:
- time.sleep(0.01)
- else:
- if STATUS != 1:
- print("Passing by (%.1f cm)" % Distance)
- import subprocess
- subprocess.call(["/root/rcswitch-pi/send", "10000", "1", "1"])
- wait(5)
- STATUS = 1
- else:
- subprocess.call(["/root/rcswitch-pi/send", "10000", "1", "0"])
- wait(5)
- STATUS = 0
- except KeyboardInterrupt:
- print("Stopped by user")
- GPIO.cleanup()
- if __name__ == '__main__':
- # use GPIO pin numbering convention
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(GPIOTrigger, GPIO.OUT)
- GPIO.setup(GPIOEcho, GPIO.IN)
- GPIO.output(GPIOTrigger, False)
- main()
- def MeasureDistance():
- StartTime = time.time()
- GPIO.output(GPIOTrigger, True)
- while GPIO.input(GPIOEcho) == 0:
- pass
- # Found the raising front
- StopTime = time.time()
- GPIO.output(GPIOTrigger, False)
- try:
- Distance = MeasureDistance()
- while True:
- while Distance > 10.0:
- Distance = MeasureDistance()
- wait(5)
- switch_on()
- while Distance < 10.0:
- Distance = MeasureDistance()
- wait(5)
- switch_off()
- except KeyboardInterrupt:
- while Distance > breaking_distance - tolerance:
- Distance = MeasureDistance()
- wait(5)
- switch_on()
- while Distance < breaking_distance + tolerance:
- Distance = MeasureDistance()
- wait(5)
- switch_off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement