Skip to content

Instantly share code, notes, and snippets.

@samuraicode
Forked from suhajdab/NeoPixel Color Twinkle
Last active August 29, 2015 14:23
Show Gist options
  • Save samuraicode/6bba7e825827c2496dfd to your computer and use it in GitHub Desktop.
Save samuraicode/6bba7e825827c2496dfd to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#define PIN 2
#define Pixels 25
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
float redStates[Pixels];
float blueStates[Pixels];
float greenStates[Pixels];
float fadeRate = 0.96;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
for(uint16_t l = 0; l < Pixels; l++) {
redStates[l] = 0;
greenStates[l] = 0;
blueStates[l] = 0;
}
}
void loop () {
if (random(20) == 1) {
uint16_t i = random(Pixels);
if (redStates[i] < 1 && greenStates[i] < 1 && blueStates[i] < 1) {
redStates[i] = random(256);
greenStates[i] = random(256);
blueStates[i] = random(256);
}
}
for(uint16_t l = 0; l < Pixels; l++) {
if (redStates[l] > 1 || greenStates[l] > 1 || blueStates[l] > 1) {
strip.setPixelColor(l, redStates[l], greenStates[l], blueStates[l]);
if (redStates[l] > 1) {
redStates[l] = redStates[l] * fadeRate;
} else {
redStates[l] = 0;
}
if (greenStates[l] > 1) {
greenStates[l] = greenStates[l] * fadeRate;
} else {
greenStates[l] = 0;
}
if (blueStates[l] > 1) {
blueStates[l] = blueStates[l] * fadeRate;
} else {
blueStates[l] = 0;
}
} else {
strip.setPixelColor(l, 0, 0, 0);
}
}
strip.show();
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment