Button Blink

This program adds user-input to the blink program. The LED only blinks while the user holds down the push-button on the Quick2Wire interface board. The push-button is connected to GPIO pin P0 by a jumper.

#!/usr/bin/env python3
 
from quick2wire.gpio import pins, In, Out
from itertools import cycle
from time import sleep
 
button = pins.pin(0, direction=In)
led = pins.pin(1, direction=Out)
 
with button, led:
    for v in cycle([1,0]):
        led.value = v * button.value
        sleep(0.5)
 

However, there's a flaw in this program. If you repeatedly press and release the button you can see that the program can take up to half a second to react. Ideally the program would react to the button immediately. To do so it must handle GPIO interrupts in an event loop, which we'll look at in the next example.

Generated with Code Guide.