APA102 LED Strip Christmas Tree Prototype

I wanted to play with addressable RGB LED strips and thought a Christmas decoration would be appropriate. I've started off with two APA102 60-LED strips and now that I see how easy it is to get acceptable results I have ordered three more strips to make a more tree-sized decoration.
The project is done in Micropython which I believe is appropriate since Arduino officially support it now although I am using a R Pi Pico W since I don't yet have an Arduino RP2040 board.
For proof of concept, I wrote a couple of functions that simulate a traditional Christmas tree with blinking colored lights. I am using an APA102 module that is a modification of the Adafruit DotStar module converted from CircuitPython to MicroPython.
A function to generate a list of random numbers between 0 and number of pixels used as addresses for the LED strip pixels. A function that uses the array in thirds to populate the "tree" with randomly blinking lights.

# rp2040 MicroPython		
import time
import random
from machine import Pin, SPI
from dotstar import DotStar

number_of_dots = 120

spi = SPI(1,2000000,sck=Pin(10), mosi=Pin(11), miso=Pin(8)) # Configure SPI
# pins correspond to R Pi Pico GPIO
# Using a DotStar Digital LED Strip with 60 LEDs/strip connected to SPI
dots = DotStar(spi, number_of_dots, brightness=.5)
	
def random_array(size):
    temp = []    # This list will have "size" randomised pixel addresses
    count = 0
    while count < size:
        roll = random.randrange(0,size) # roll the dice. Random number used as pixel address
        if roll not in temp:            # if there is no collision
            temp.append(roll)           # save 
            count += 1                  # do for size of array
    return temp
    
# classic Christmas tree with blinking multicolored lights.
# Create a random list of pixel addresses for LED_strip.
# Populate the "tree" with red, green, and blue pixels and repeat for 
# a changing pattern
def classic_tree(LED_strip):
    tree = random_array(120)    # random array of locations
    for loc in range(0,40):
        LED_strip[tree[loc]] = (128,0,0)     # write red lights
    for loc in range(40,80):
        LED_strip[tree[loc]] = (0,128,0)     # write green lights
    for loc in range(80,120):
        LED_strip[tree[loc]] = (0,0,128)     # write blue lights

# main program
while True:
    classic_tree(dots)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.