Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#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);
}

Hej Balázs,

månge tak for this gist. This makes a nice pattern on my 3 neopixels ring.

best regards and merry christmas
Andreas

How does the faderate variable affect the display of colours on the neopixel?

chrisw63 commented Dec 9, 2017

Using the single letter 'l' (as in j, k, l...) as a variable is not good practice. Way too easy to get it mixed up with the number 1 in many fonts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment