Danke für eure Tipps und Hinweise,
hier nun der überarbeitete Code:
Code:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_TRIGGER = 14
GPIO_ECHO = 15
adjustment = 7
print "Ultraschallmessung mit SRF04"
while True:
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
GPIO.setup(GPIO_ECHO,GPIO.IN)
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
start = time.time()
GPIO.output(GPIO_TRIGGER, False)
while GPIO.input(GPIO_ECHO)==0:
pass
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
pass
stop = time.time()
elapsed = stop-start
distance = elapsed * 23000
distance = distance - adjustment
print "Abstand: %.1f cm" % distance
time.sleep(1)
GPIO.cleanup()
Lesezeichen