Hello
I recently purchased a plasma 2040 to control some Christmas LED lights in my window. It is really nice piece of kit and I got it working well with some basic micropython code and the examples. But I am struggling to get a piece of code to do exactly what I want.
What I would like is when the 2040 is turned on for it to display a red green flashing pattern, but then when each of the buttons are pressed for that pattern to change to a new pattern.
My current code (see below), works but when you plug it in it shows no pattern until you push a button and then you have to stop that sequence before starting a new one with another button push. I believe it is the while True loop that is causing the issues but I don’t know how to stop the while true loop at the button push and then to initial the new LED patterns.
The example in the documentation for changing the LED by button push works for static LED but doesn’t work for a while True loop of moving LEDs.
I suppose my real question is how to I break out of a while true loop (to end the current pattern) using a button push and at the same time use that same button push to instigate a new pattern on the LEDs
> import time
> import plasma
> # Import plasma2040
> from plasma import plasma2040
> # Import helpers for Buttons
> from pimoroni import Button
>
> NUM_LEDS = 161
> led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
>
> user_sw = Button(plasma2040.USER_SW)
> button_a = Button(plasma2040.BUTTON_A)
> button_b = Button(plasma2040.BUTTON_B)
>
> # Set up brightness (between 0 and 1)
> BRIGHTNESS = 0.5
> SPEED = 1
>
> #Definition for alernating red green pattern code taken from examples works
> def alt_blink():
> HUE_1 = 0
> HUE_2 = 127
> while True:
> for i in range(NUM_LEDS):
> # the if statements below use a modulo operation to identify the even and odd numbered LEDs
> if (i % 2) == 0:
> led_strip.set_hsv(i, HUE_1 / 360, 1.0, BRIGHTNESS)
> else:
> led_strip.set_hsv(i, HUE_2 / 360, 1.0, BRIGHTNESS)
> time.sleep(SPEED)
>
> for i in range(NUM_LEDS):
> if (i % 2) == 0:
> led_strip.set_hsv(i, HUE_2 / 360, 1.0, BRIGHTNESS)
> else:
> led_strip.set_hsv(i, HUE_1 / 360, 1.0, BRIGHTNESS)
> time.sleep(SPEED)
> #pushing the boot button stops this so another button can then be pushed.
> if user_sw.read():
> break
>
> # definition for yellow on off alternating pattern modified from example works
> def flash():
> HUE_1 = 0
> HUE_2 = 60
>
> while True:
> for i in range(NUM_LEDS):
> # the if statements below use a modulo operation to identify the even and odd numbered LEDs
> if (i % 2) == 0:
> led_strip.set_hsv(i, HUE_1 / 360, 1.0, 0)
> else:
> led_strip.set_hsv(i, HUE_2 / 360, 1.0, BRIGHTNESS)
> time.sleep(0.2)
>
> for i in range(NUM_LEDS):
> if (i % 2) == 0:
> led_strip.set_hsv(i, HUE_2 / 360, 1.0, BRIGHTNESS)
> else:
> led_strip.set_hsv(i, HUE_1 / 360, 1.0 , 0)
> time.sleep(0.2)
> #pushing the boot button stops this so another button can then be pushed.
> if user_sw.read():
> break
>
>
> # Start updating the LED strip
> led_strip.start()
>
> while True:
> if button_a.read():
> alt_blink()
> if button_b.read():
> flash()