David Middlecamp
Published © MIT

Holiday Cheer Lights

Power your lights with Holiday Cheer from the Internet!

Full instructions provided574
Holiday Cheer Lights

Story

Read more

Code

holiday_strip.cpp

C/C++
Our starter firmware
// This #include statement was automatically added by the Spark IDE.
#include "neopixel/neopixel.h"

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 300
#define PIXEL_TYPE WS2812B

int reds[PIXEL_COUNT];
int greens[PIXEL_COUNT];
int blues[PIXEL_COUNT];

//setup the strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int idx = 0, a = 0;
int cheer = 0;
double brightness = 1;

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Serial.begin(9600);
  Spark.subscribe("cheer", onCheer);
}

void onCheer(const char *topic, const char *data) {
    Serial.println("detected cheer");
    cheer += 5;
}

void loop(){
    if (idx >= PIXEL_COUNT) {
        idx = 0;
    }
    if (a >= PIXEL_COUNT) {
        a = 0;
    }
    
    int energy = (cheer <= 0) ? random(32) : 191 + random(64);
    bool state = random(2) == 1;
    reds[idx] = (state) ? energy : 0;
    greens[idx] = (!state) ? energy : 0;
    blues[idx] = (cheer > 0) ? random(255) : 0;


    int numPixels = strip.numPixels();
    
    for(int i=0;i<numPixels;i++) {
        uint32_t color = strip.Color(reds[i], greens[i], blues[i]);
        
        if (cheer > 0) {
             color = strip.Color(random(255), random(255), random(255));
        }
        
        // addressable strip
        strip.setPixelColor((i+a)%numPixels, color);
        
        // 
        //b.ledOn((i+a)%12, reds[i] * brightness, greens[i] * brightness, blues[i] * brightness);
    }
    strip.show();

    

    a++;
    idx++;
    //state = !state;
    if (cheer > 0) {
        cheer--;
    }
    
    // if we have buttons
    //bool anyButton = b.buttonOn(1) || b.buttonOn(2) || b.buttonOn(3) || b.buttonOn(4);
    // if (anyButton) {
    //     Spark.publish("cheer", "5");
    //     delay(100);
    // }

    //Wait a mo'
    delay(50);
}

Credits

David Middlecamp

David Middlecamp

8 projects • 71 followers
I help people build amazing things, I ship solutions to hard problems.

Comments