IF/ELSE question help Micropython programming #9948
Replies: 1 comment 7 replies
-
The problem here is that while your code is running the The way to solve this is to either use asyncio, and run the effects in a separate task to the code that checks the pins, or to make the buttons raise interrupts instead. See https://docs.micropython.org/en/latest/library/machine.Pin.html#machine.Pin.irq This will allow you to set a function that will be called when the button is pressed, even if it's in the middle of doing the animation or sleeping. From there you can set a global variable to stop the effect, and check that while you do the animation or sleep. You will need to rearrange your code a bit, but perhaps the least invasive way to do this is something like. (Untested, but hope you get the idea) next_button = 0
def button_press(p):
global next_button
if p == button1:
next_button = 1
#... elif p == button2 (and button3)
# change each of these effect functions to ensure that the most recent button was still the one that started this effect
def color_chase5(color, wait, btn):
for i in range(NUM_LEDS):
if next_button != 0 and next_button != btn: # stop this effect if a new button has been pressed, and it's different to the current one
return
pixels_set(i, color)
time.sleep(wait)
pixels_show5()
button1.irq(handler=button_press, trigger=Pin.IRQ_FALLING)
button2.irq(handler=button_press, trigger=Pin.IRQ_FALLING)
button3.irq(handler=button_press, trigger=Pin.IRQ_FALLING)
def handle_button1():
print("country1")
relay1.off()
led.value(0)
color_chase1(BLUE, 0.2, 1)
print("country1 ledstrip1 done")
# 250ms sleep in 25ms chunks
for i in range(10):
if next_button != 0 and next_button != 1:
return
time.sleep_ms(25) # <--- fixed indentation
pixels_fill(BLACK)
color_chase2(BLUE, 0.2, 1)
print("country1 ledstrip2 done")
# 2000ms sleep in 20ms chunks
for i in range(100):
if next_button != 0 and next_button != 1:
return
time.sleep_ms(20) # <--- fixed indentation
relay1.on()
try:
while(1):
current_button = next_button
next_button = 0
if current_button == 1:
handle_button1()
elif current_button == 2:
....
elif ...
....
else:
# reset to black
except:
pass |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. I'm new to Micropython programming. I'm doing Crestron programming for room automation and this is really different for me with Micropython. But thing progress ;-)
I have an question about interact/stopping a IF / Else programming
I explain more:
I have setup a Rasbery pico with 3 button and 3 run of 50xWS2811 led light
If I press button #1 the 1st 50 led light up one after one and stay open for 5 secondes
If I press button #2 the 1st 50 led light up one after one, after done the second 50 led light un one after one and they all stay on 5 sec
If I press button #3 the 1st 50 led light up one after one, after done the second 50 led light un one after one, after done the third 50 led light un one after one and they stay on 5 sec.
Waht I try to achived here and i'm not able to do it. If I press button #1 it start the light up process....but I want stop it and restard an other process if I press button #2 or button #3...
For now the programm work except the ''stop'' function I cannot stop my light to light up by pressing other button.
thanks
Carl
Map monde v1.22.zip
Beta Was this translation helpful? Give feedback.
All reactions