Plasma 2040 code help

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()

I “think” (not an expert ;) ) you need to take the first while True: out. And adjust all the indents below it.

Everything above the while True gets run once on bootup. What’s in the while True loops / repeats.

I’m pretty sure one of them has to go.

Thanks, just looking at it that while true has pasted incorrectly it is actually part of the def flash() function.
I have updated the original post with the correction.

Just new to the plasma 2040 and LED strip lighting so in the above set_hsv() what does the /360 signify? I khow HUE can be between 1and 360.
Also is there a web page or similar where these functions/commands are listed and explained (I am trying to forget BASIC!!!)

Sorry 0 and 360! poor typing skills!

Hey GeoffW

There are two places to find info about the plasma 2040:

and

The second is probably more helpful for coding.

As for your question about my code, as the .set_hsv function required all values to be less than 1.0,
I am taking my variable HUE_2 (Which as you correctly pointed out could be between 0 and 360) and dividing it by 360, so it is less than 1 to allow the function to work.

Hope this helps.

Thanks for your quick response. Thats what i didnt know that the HUE entry had to be less than 1.0 . Couldn’t find anywhere to explain what the entry size limitations needed to be. It would say HUE, SATURATION, BRIGHTNESS etc but not size. Hence the Conversion from hue values to 0.0 to1.0 values as you say. I presume the first entry can be greater as its the LED number. All great fun!!! thanks again.

Your welcome, yes the first value can be any number up to the maximum number of LEDs in your strip-1 as the indexing will start from 0 in python. So if you have 10 LEDs the first will be 0 and the last 9.

Thanks again for your help. Getting the hang of things now (although indents still give me grief!). I’ve arranged my strip of 50 LEDS into a 5 point star pinned to a wall! with the leds numbering from 0 (bottom left point), 30(left point), 10 (top point), 40(right point) and 20(bottom right) point and the other leds in sequence around the shape. Hope that makes sense. I then drew the star shape on to paper to see where each led in the shape would lie in each star arm and centre pentagon. I can now give myself plentiful headaches working out numerous patterns of lights.:-)

_
image

1 Like

That looks really great. What LEDs are those? They aren’t strips looks like they could work nicely for a project I have in mind.

It’s just those LED lights listed in the “Getting started with the Plasma 2040” article. WS2812 if memory serves. I have a 5 metre length of 50 LEDs so each point to point of the star is a metre. I’ve got a set on WS 2815b LEDs but to use those I need to sort a 12v supply that got enough umph. I’ve written a couple of scripts for that pattern of lights so I’ll post them tomorrow. They are probably not to elegant but once I get into microP I can tidy them up a bit.

The 2812 leds are just thin wires between leds unlike those mounted on the plastic strips

Ah yes I had been after some of those for a while. Thanks

No probs. Always ready to help if I can

Deleted wrong code posted

Starting at LEDs 0,10,20,30,40 a simple colour progression to the next star point. Then rinse and repeat!

import plasma
import time
from plasma import plasma2040

NUM_LEDS=50

led_strip=plasma.WS2812(NUM_LEDS,0,0,plasma2040.DAT)

Start=(0,1,2)
PointsIn=(0,10,20,30,40,1,11,21,31,41,2,12,22,32,42,3,13,23,33,43,4,14,24,34,44,5,15,25,35,45,6,16,26,36,46,7,17,27,37,47,8,18,28,38,48,9,19,29,39,49)
led_strip.start()

x=1

while True:

    for i in PointsIn:
        if i >-1 and i<10:
            r=0
            g=0
            b=255
        if i >9 and i<20:
            r=255
            g=0
            b=0
        if i >19 and i<30:
            r=0
            g=255
            b=0
        if i >29 and i<40:
            r=255
            g=0
            b=255
        if i >39 and i<50:
            r=255
            g=255
            b=0                
        led_strip.set_rgb(i,r,g,b)
        time.sleep(0.01)
          
    for i in range (NUM_LEDS):
        led_strip.set_rgb(i,0,0,0)

x+=1