Jun-28-2018, 01:34 PM
Hello guys. On my raspberry pi I have a hc-sr04 ultrasonic sensor, now I want to run a sh script, when the sensor measurement is below 10cm. Here is my Code, but it doesnt work. It only executes the code when I press ctrl c. PS Iam new to python
#!/usr/bin/env python import time import RPi.GPIO as GPIO import os GPIOTrigger = 23 GPIOEcho = 24 def MesseDistanz(): GPIO.output(GPIOTrigger, True) time.sleep(0.00001) GPIO.output(GPIOTrigger, False) StartZeit = time.time() StopZeit = StartZeit while GPIO.input(GPIOEcho) == 0: StartZeit = time.time() while GPIO.input(GPIOEcho) == 1: StopZeit = time.time() SignalLaufzeit = StopZeit - StartZeit Distanz = (SignalLaufzeit/2) * 34350 return [Distanz, (SignalLaufzeit*1000/2)] def main(): try: while True: Ergebnis = MesseDistanz() print("Gemessene Entfernung: %.1f cm (Signallaufzeit: %.4fms)" % (Ergebnis[0], Ergebnis[1])) time.sleep(1) except KeyboardInterrupt: print("Messung abgebrochen") GPIO.cleanup() if __name__ == '__main__': GPIO.setmode(GPIO.BCM) GPIO.setup(GPIOTrigger, GPIO.OUT) GPIO.setup(GPIOEcho, GPIO.IN) GPIO.output(GPIOTrigger, False) main() if 0 < 10: os.system('. /home/pi/test.sh')