Was ist an diesem Python Code falsch?

1 Antwort

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