Pimoroni_Pico_Display_Pack_documentation
Pimoroni_Pico_Display_Pack_documentation copied to clipboard
Interup
Hi,
Thanks for the great job. It help me to get started.
Here is a piece of code to do interup with button A
` from machine import Pin from time import sleep
stop = False
def handle_interrupt(pin): global stop stop = True global interrupt_pin interrupt_pin = pin
ButtonA = Pin(12, Pin.IN, Pin.PULL_UP) ButtonA.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True: if stop: print('press detected! Interrupt caused by:', interrupt_pin) #led.value(1) sleep(2) #led.value(0) print('Resuming ...') stop = False
print('Running') sleep(0.1) `
Thanks for the example! I'll work it into the docs soonish. I'm in two minds about whether or not to spin it into a separate file as "advanced examples" as it's getting quite big, and there's a second approach to buttons using the second core on the Pico. I'll have a think.
Thanks for that - I found it very useful to control the backlight on my Pimoroni PicoDisplay.
Apart from using IRQ_FALLING as pressing a button pulls the GPIO pin low :-
Cheers, Scott
backlight = 0.5
def bl_up(pin):
global backlight
backlight = backlight + 0.05
if backlight > 1.0:
backlight = 1.0
display.set_backlight(backlight)
def bl_dn(pin):
global backlight
backlight = backlight - 0.05
if backlight < 0.0:
backlight = 0.0
display.set_backlight(backlight)
ButtonA = Pin(12, Pin.IN, Pin.PULL_UP)
ButtonA.irq(trigger=Pin.IRQ_FALLING, handler=bl_up)
ButtonB = Pin(13, Pin.IN, Pin.PULL_UP)
ButtonB.irq(trigger=Pin.IRQ_FALLING, handler=bl_dn)