#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
from RPi import GPIO
def MeasureDistance(trigger, echo):
GPIO.output(trigger, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(trigger, GPIO.LOW)
StartTime = time.time()
while GPIO.input(echo) == 0:
StartTime = time.time()
while GPIO.input(echo) == 1:
StopTime = time.time()
TimeElapsed = StopTime - StartTime
Distance = (TimeElapsed * 34300) / 2
return Distance
def main():
distr = 0
distl = 0
while GPIO.input(GPIOKNOPF) == 1:
time.sleep(0.1)
try:
while True:
Distance1 = MeasureDistance(GPIOTrigger_S1, GPIOEcho_S1)
Distance2 = MeasureDistance(GPIOTrigger_S2, GPIOEcho_S2)
if Distance1 > 40:
distr = Distance1
elif distr > 40:
pass
if Distance2 > 40:
distl = Distance2
elif distl > 40:
pass
time.sleep(0.2)
except KeyboardInterrupt:
print("Measurement stopped by user")
GPIO.cleanup()
if __name__ == '__main__':
GPIOTrigger_S1 = 35
GPIOEcho_S1 = 37
GPIOTrigger_S2 = 40
GPIOEcho_S2 = 36
GPIOKNOPF = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIOTrigger_S1, GPIO.OUT)
GPIO.setup(GPIOEcho_S1, GPIO.IN)
GPIO.setup(GPIOTrigger_S2, GPIO.OUT)
GPIO.setup(GPIOEcho_S2, GPIO.IN)
GPIO.setup(GPIOKNOPF, GPIO.IN)
main()
#EOF