Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Forked from suhajdab/NeoPixel Color Twinkle
Last active August 25, 2017 07:01
Show Gist options
  • Save mnemocron/34c18e1e22e82702faa47f91948e571d to your computer and use it in GitHub Desktop.
Save mnemocron/34c18e1e22e82702faa47f91948e571d to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 8
#define COLOR_PRESET 0x00F02000 // orange
// 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)
// NEO_GRBW Pixels are wired for GRBW bitstream (SK6812 RGBW LEDs)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + 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.
uint8_t rStates[NUM_LEDS];
uint8_t bStates[NUM_LEDS];
uint8_t gStates[NUM_LEDS];
float fadeRate = 0.04;
uint8_t rPreset = ((COLOR_PRESET >> 16)&0xff);
uint8_t gPreset = ((COLOR_PRESET >> 8)&0xff);
uint8_t bPreset = ((COLOR_PRESET )&0xff);
void setup() {
strip.begin();
strip.show();
for(uint16_t i = 0; i < NUM_LEDS; i++) {
rStates[i] = 0;
gStates[i] = 0;
bStates[i] = 0;
}
}
void loop () {
if (random(20) == 1) {
uint16_t i = random(NUM_LEDS);
if (rStates[i] < 1 && gStates[i] < 1 && bStates[i] < 1) {
rStates[i] = rPreset;
gStates[i] = gPreset;
bStates[i] = bPreset;
}
}
for(uint16_t i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, rStates[i], gStates[i], bStates[i]);
if ( rStates[i] >= 1 || gStates[i] >= 1 || bStates[i] >= 1 ){
if ( rStates[i] >= 1 ){
rStates[i] -= rStates[i]*fadeRate;
} else {
rStates[i] = 0;
}
if ( gStates[i] >= 1 ){
gStates[i] -= gStates[i]*fadeRate;
} else {
gStates[i] = 0;
}
if ( bStates[i] >= 1 ){
bStates[i] -= bStates[i]*fadeRate;
} else {
bStates[i] = 0;
}
}
}
strip.show();
delay(10);
}
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 8
#define COLOR_PRESET 0x00F05000
#define COLOR_GLOW 0x00060010
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
uint8_t rStates[NUM_LEDS];
uint8_t bStates[NUM_LEDS];
uint8_t gStates[NUM_LEDS];
float fadeRate = 0.03;
uint8_t rPreset = ((COLOR_PRESET >> 16)&0xff);
uint8_t gPreset = ((COLOR_PRESET >> 8)&0xff);
uint8_t bPreset = ((COLOR_PRESET )&0xff);
uint8_t rGlow = ((COLOR_GLOW >> 16)&0xff);
uint8_t gGlow = ((COLOR_GLOW >> 8)&0xff);
uint8_t bGlow = ((COLOR_GLOW )&0xff);
void setup() {
strip.begin();
strip.show();
for(uint16_t i = 0; i < NUM_LEDS; i++) {
rStates[i] = 0;
gStates[i] = 0;
bStates[i] = 0;
}
}
void loop () {
if (random(30) == 1) {
uint16_t i = random(NUM_LEDS);
if (rStates[i] <= rGlow && gStates[i] <= gGlow && bStates[i] <= bGlow) {
rStates[i] = rPreset + random(16);
gStates[i] = gPreset + random(16);
bStates[i] = bPreset + random(16);
}
}
for(uint16_t i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, rStates[i], gStates[i], bStates[i]);
if ( (rStates[i] >= rGlow) || (gStates[i] >= gGlow) || (bStates[i] >= bGlow) ){
rStates[i] > rGlow ? rStates[i] -= rStates[i]*fadeRate : rStates[i] = rGlow;
gStates[i] > gGlow ? gStates[i] -= gStates[i]*fadeRate : gStates[i] = gGlow;
bStates[i] > bGlow ? bStates[i] -= bStates[i]*fadeRate : bStates[i] = bGlow;
} else {
strip.setPixelColor(i, rGlow, gGlow, bGlow);
}
}
strip.show();
delay(10);
}
@mnemocron
Copy link
Author

mnemocron commented Aug 20, 2017

Differences to master:

NeoPixelTwinkle.ino
Functionality - use one preset color instead of random colors
Structure - using uint8_t (8 bits) data types instead of float (32 bits) for the rgbState values

NeoPixelTwoTwinkle.ino
Functionality - use one preset color for the sparks and one (darker) preset color to fade back to (instead of black/off)
Structure - using uint8_t (8 bits) data types instead of float (32 bits) for the rgbState values

(also the constructor uses uses WRGB strips)

Demo

neopixeltwocolortwinkle ino
Running NeoPixelTwoTwinkle.ino


Tags:

effect animation sparkly sparkling sparkles twinkle blinky blinking fading fade

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