Was ist an diesem Python Code falsch?
Hallo.
Was ist der Fehler in diesem Programm?
Es ist ein Programm um eine Wählscheibe eines Telefons auszuwerten, also ein Impulszähler.
Ich bekomme immer nur irgendwelche Zahlen raus, die nichts mit der Eingabe zu tun haben.
Liegt es daran dass ein Impuls nur 100 ms dauert?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
b = 0
while b == 0:
if GPIO.input(18) == GPIO.HIGH:
a = 0
while a <= 10:
if GPIO.input(16) == GPIO.HIGH:
a = a + 1
if GPIO.input(16) == GPIO.HIGH:
while b == 0:
if GPIO.input(16) != GPIO.HIGH:
break
else:
continue
elif GPIO.input(18) == GPIO.HIGH:
print(a)
break
else:
continue
else:
continue
1 Antwort
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
Computer, Informatik, Python
Ich räume erst mal auf:
# b = 0
while GPIO.input(18) == GPIO.HIGH:
a = 0
while a <= 10:
if GPIO.input(16) == GPIO.HIGH:
a = a + 1
while GPIO.input(16) != GPIO.HIGH:
pass # wait for end of pulse
elif GPIO.input(18) == GPIO.HIGH:
print(a)
break
Das elif verstehe ich nicht. Sollte das Ende der Zahl nicht durch (18)==LOW angezeigt werden? Diese Bedingung kannst Du einfach bei a<=10 ergänzen. Dann brauchst Du auch die äußere while-Schleife nicht mehr:
a = 0
while a <= 10 and GPIO.input(18) == GPIO.HIGH:
if GPIO.input(16) == GPIO.HIGH:
a = a + 1
while GPIO.input(16) != GPIO.HIGH:
pass # wait for end of pulse
print(a) # 1-10
a %= 10 # -> 1-9 0