Pico/micropython Switch callbacks
Posted: Wed Apr 28, 2021 5:48 pm
Hi,
I thought I would try a Pico to power a number of led circuits to light a model. I have managed to get a set of leds to power on and another set to flash via a while true loop. I have tried to expand this to work off a momentary switch - as a test I tried this code (to switch on an led, pause, then switch on some flashing leds and then switch everything off :
However, I tried to get clever and it all went wrong! I defined a global 'lights' and set it to True; I changed the while loop to read "while lights" - this ran OK but obviously never ended. So I set up another switch and coded that to change the value of the global to False hoping that it would stop the while loop. But it didn't. Can someone tell me the behaviour of the callbacks? If I press the switch that starts the lights running, does the program get 'stuck' in the while loop and not recognise the press of the second button?
Any help would be appreciated! Sorry, but I lost the code showing both buttons (but I'll recreate it and paste here if required)
Thanks,
Steve
I thought I would try a Pico to power a number of led circuits to light a model. I have managed to get a set of leds to power on and another set to flash via a while true loop. I have tried to expand this to work off a momentary switch - as a test I tried this code (to switch on an led, pause, then switch on some flashing leds and then switch everything off :
Code: Select all
from machine import Pin
import utime
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
deck_lights = Pin(13, Pin.OUT)
nav_lights = Pin(15, Pin.OUT)
def button_callback(pin):
# Switch on the deck lights
deck_lights.value(1)
utime.sleep(2)
i = 0
while i < 10:
nav_lights.value(0)
utime.sleep(1)
nav_lights.value(1)
utime.sleep(0.1)
i+=1
nav_lights.value(0)
utime.sleep(2)
deck_lights.value(0)
button.irq(button_callback, Pin.IRQ_FALLING)
Any help would be appreciated! Sorry, but I lost the code showing both buttons (but I'll recreate it and paste here if required)
Thanks,
Steve