Skip to content

Instantly share code, notes, and snippets.

@shfitz
Created June 14, 2017 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shfitz/23a8b27e397ecdeba8a5bfe2dffea6da to your computer and use it in GitHub Desktop.
Save shfitz/23a8b27e397ecdeba8a5bfe2dffea6da to your computer and use it in GitHub Desktop.
BlinkyTape sparkle
/*
Creates a randomly sparkling effect on an arbitrary number of
LEDs using the WS2812 driver.
*/
#include <Adafruit_NeoPixel.h> // include the neopixel library
// most people use FastLED for the blinkytape, but the Adafruit
// library is more my speed
#define PIN 13 // we're using the blinkytape, data is pin 13
const int numLed = 60; // number of lights
int ledBuffer[numLed]; // which LED are we talking to?
float ledSpeed[numLed]; // how fast is this one fading?
int ledDir[numLed]; // what direction is the LED going?
int ledMax[numLed]; // led max brightness
int ledDeathCount[numLed]; // is the light on or off?
// scale the rgb levels for a yellowish tint
float redVal = 200. / 255.;
float greenVal = 190. / 255.;
float blueVal = 20. / 255.;
// update speed in ms
// higher value is slower sparkle
int speedVar = 5;
// instantiate the library
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLed, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
delay(5000); // pause for the 32u4 to startup
// populate the LED arrays with random speed, max brightness
// give direction and death meaning
for (int i = 0; i < numLed; i++) {
ledSpeed[i] = float(random(5, 75) / 100.);
ledMax[i] = ledSpeed[i] * 255;
ledDir[i] = 1;
ledDeathCount[i] = 0;
}
strip.begin(); // init the strip
strip.show(); // blank the strip
}
void loop() {
// upadate the LED values
for (int i = 0; i < 60; i++) {
updateLed(i);
}
// write the values to the strip
for (int i = 0; i < 60; i++) {
strip.setPixelColor(i, int(ledBuffer[i] * redVal), int(ledBuffer[i] * greenVal), int(ledBuffer[i] * blueVal));
}
strip.show(); // push the values to the strip
delay(speedVar); // pause
}
void updateLed(int led) {
// any value in deathCount indicates the LED is dead
if (ledDeathCount[led] != 0) { // if it's dead
ledDeathCount[led]++; // keep dying
// higher numbers = fewer lights on at a time
if (ledDeathCount[led] == 120) { // if it has been dead for awhile
ledDeathCount[led] = 0; // bring it back to life
}
} else {
if (ledBuffer[led] > ledMax[led]) { // if the current value is greater than the max
ledDir[led] = -1; // start the fade
}
if (ledBuffer[led] == 0) { // if the value is 0
ledDeathCount[led] = 1; // LED is dead
ledSpeed[led] = float(random(5, 75) / 100.); // give it a new speed
ledMax[led] = ledSpeed[led] * 255; // a new max value based on the speed
ledDir[led] = 1; // and reset the direction
}
ledBuffer[led] = ledBuffer[led] + ledDir[led] + ledSpeed[led]; // set the value of the LED in the array
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment