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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#!/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' ) |