Blinged my xmas tree

Hi!

Figured I’d share my evening project with you.

We had some basic (LED) lights up in our tree this year and at dinner time, I figured I could use an Arduino and some addressable LEDs that I had lying around to bling it up a little!

First I had to figure out what kind of LEDs these were and after a bit of research I found out that they were WS2801s. (5 volt, but with separate clock and data lines!) I quickly created a project in PlatformIO and got to work. Luckily, this was supported by the NeoPixelBus library.

After a few iterations, I got the thing up and running and this was the result:

This is the resulting code:

#include <NeoPixelBus.h>

#define NUM_LEDS 50
#define MAX_SPARK_INTERVAL 25
#define MAX_BALL_INTERVAL 25

const uint8_t DataPin = 2;
const uint8_t ClockPin = 3;

#define MAX_COLOR_SATURATION 128

NeoPixelBus<NeoRgbFeature, NeoWs2801Method> strip(NUM_LEDS, ClockPin, DataPin);

RgbColor green(0, MAX_COLOR_SATURATION, 0);

int balls[NUM_LEDS];
int sparks[NUM_LEDS];

uint32_t loop_counter = 0;

void setup()
{
    Serial.begin(115200);
    while (!Serial)
        ; // wait for serial attach

    Serial.println();
    Serial.println("Initializing...");
    Serial.flush();

    // this resets all the neopixels to an off state
    strip.Begin();
    strip.Show();

    Serial.println();
    Serial.println("Running...");
}

void loop()
{
    // First fill it out with green
    for (uint16_t i = 0; i < NUM_LEDS; i++)
    {
        strip.SetPixelColor(i, green);
    }

    // Fade the balls
    for (int b = 0; b < NUM_LEDS; b++)
        if (balls[b] > 0)
        {
            balls[b]--;

            // Sanity check
            if (balls[b] < 0)
                balls[b] = 0;
        }

    // Fade the sparks
    for (int s = 0; s < NUM_LEDS; s++)
        if (sparks[s] > 0)
        {
            sparks[s] -= 5;

            // Sanity check
            if (sparks[s] < 0)
                sparks[s] = 0;
        }

    // If we have a sufficiently random interval, create a new spark
    if (loop_counter % random(MAX_SPARK_INTERVAL) == 0)
    {
        int l = random(NUM_LEDS);
        sparks[l] = MAX_COLOR_SATURATION;
        Serial.println("Sparked: " + String(l));
    }

    // If we have a sufficiently random interval, create a new "ball"
    if (loop_counter % random(MAX_BALL_INTERVAL) == 0)
    {
        int l = random(NUM_LEDS);
        balls[l] = MAX_COLOR_SATURATION;
        Serial.println("Balled: " + String(l));
    }

    // Update the balls
    for (int b = 0; b < NUM_LEDS; b++)
        if (balls[b] > 0)
            strip.SetPixelColor(b, RgbColor(balls[b], 0, 0));

    // Update the sparks
    for (int s = 0; s < NUM_LEDS; s++)
        if (sparks[s] > 0)
            strip.SetPixelColor(s, RgbColor(sparks[s], sparks[s], sparks[s]));

    strip.Show();

    delay(125);

    loop_counter++;
}
7 Likes

thanks nick!

1 Like