Skip to content

Instantly share code, notes, and snippets.

@hekras
Created May 23, 2021 16:29
Show Gist options
  • Select an option

  • Save hekras/422d76a68442c84d90429a9ded4548cb to your computer and use it in GitHub Desktop.

Select an option

Save hekras/422d76a68442c84d90429a9ded4548cb to your computer and use it in GitHub Desktop.
// NeoPixel test program showing use of the WHITE channel for RGBW
// pixels only (won't look correct on regular RGB NeoPixel strips).
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 14
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 16
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 255
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 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_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
uint8_t rrr[16];
uint8_t ggg[16];
uint8_t bbb[16];
uint8_t drrr[16];
uint8_t dggg[16];
uint8_t dbbb[16];
void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS); // Set BRIGHTNESS to about 1/5 (max = 255)
// init colors in the strip
for(int i=0;i<16;i++){
rrr[i] =
ggg[i] =
bbb[i] = 0;
strip.setPixelColor(i, strip.Color(rrr[i], ggg[i], bbb[i]));
}
strip.show();
}
void loop() {
uint8_t rrr2;
uint8_t ggg2;
uint8_t bbb2;
uint8_t dr;
uint8_t dg;
uint8_t db;
int ii=0;
int dii=0;
/*
for(int n=0;n<40;n++){
for(int i=0;i<16;i++){
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
delay(100);
for(int i=0;i<16;i++){
strip.setPixelColor(i, strip.Color(0, 0, 255));
}
strip.show();
delay(100);
}
*/
/*
for(int n=0;n<20;n++){
rrr2 = random(0, 255);
drrr[0] = (rrr2 - rrr[0]) / 8;
ggg2 = random(0, 255);
dggg[0] = (ggg2 - ggg[0]) / 8;
bbb2 = random(0, 255);
dbbb[0] = (bbb2 - bbb[0]) / 8;
rrr2 = random(0, 255);
drrr[15] = (rrr2 - rrr[15]) / 8;
ggg2 = random(0, 255);
dggg[15] = (ggg2 - ggg[15]) / 8;
bbb2 = random(0, 255);
dbbb[15] = (bbb2 - bbb[15]) / 8;
for(int j=0;j < 8;j++){
dr = (rrr[15] - rrr[0]) / 16;
dg = (ggg[15] - ggg[0]) / 16;
db = (bbb[15] - bbb[0]) / 16;
for(int i=0;i<16;i++){
rrr[i] = drrr[0] + dr * i;
ggg[i] = dggg[0] + dg * i;
bbb[i] = dbbb[0] + db * i;
strip.setPixelColor(i, strip.Color(rrr[i], ggg[i], bbb[i]));
}
strip.show();
delay(250);
ii = 0;
rrr[ii] += drrr[ii];
ggg[ii] += dggg[ii];
bbb[ii] += dbbb[ii];
ii = 15;
rrr[ii] += drrr[ii];
ggg[ii] += dggg[ii];
bbb[ii] += dbbb[ii];
}
}
*/
// running up and down
ii = 0;
dii = 1;
for(int n=0;n<520;n++){
dr = random(0, 255);
dg = random(0, 255);
db = random(0, 255);
for(int i=0;i<16;i++){
strip.setPixelColor(i, strip.Color(0,0,0));
}
strip.setPixelColor(ii, strip.Color(dr,dg,db));
strip.show();
delay(20);
ii += dii;
if ((ii == 0)||(ii == 15)){
dii = -dii;
}
}
// running fadeing
for(int n=0;n<120;n++){
if ((n%5) == 0){
ii = 0;
rrr[ii] = random(0, 255);
ggg[ii] = random(0, 255);
bbb[ii] = random(0, 255);
}
else{
ii = 0;
rrr[ii] = 0;
ggg[ii] = 0;
bbb[ii] = 0;
}
for(int i=0;i<16;i++){
strip.setPixelColor(i, strip.Color(rrr[i], ggg[i], bbb[i]));
}
strip.show();
delay(250);
for(int i=15;i>0;i--){
rrr[i] = rrr[i-1];
ggg[i] = ggg[i-1];
bbb[i] = bbb[i-1];
}
}
// running fadeing
for(int n=0;n<20;n++){
ii = 0;
rrr2 = random(0, 255);
drrr[ii] = (rrr2 - rrr[ii]) / 8;
ggg2 = random(0, 255);
dggg[ii] = (ggg2 - ggg[ii]) / 8;
bbb2 = random(0, 255);
dbbb[ii] = (bbb2 - bbb[ii]) / 8;
for(int i=15;i>0;i--){
rrr2 = rrr[i-1];
drrr[i] = (rrr2 - rrr[i]) / 8;
ggg2 = ggg[i-1];
dggg[i] = (ggg2 - ggg[i]) / 8;
bbb2 = bbb[i-1];
dbbb[i] = (bbb2 - bbb[i]) / 8;
}
for(int j=0; j < 8;j++){
for(int i=0;i<16;i++){
rrr[i] += drrr[i];
ggg[i] += dggg[i];
bbb[i] += dbbb[i];
strip.setPixelColor(i, strip.Color(rrr[i], ggg[i], bbb[i]));
}
strip.show();
delay(250);
}
}
// random fadomg
for(int n=0;n<20;n++){
for(int i=0;i<16;i++){
rrr2 = random(0, 255);
drrr[i] = (rrr2 - rrr[i]) / 8;
ggg2 = random(0, 255);
dggg[i] = (ggg2 - ggg[i]) / 8;
bbb2 = random(0, 255);
dbbb[i] = (bbb2 - bbb[i]) / 8;
}
for(int j=0; j < 8;j++){
for(int i=0;i<16;i++){
rrr[i] += drrr[i];
ggg[i] += dggg[i];
bbb[i] += dbbb[i];
strip.setPixelColor(i, strip.Color(rrr[i], ggg[i], bbb[i]));
}
strip.show();
delay(250);
}
}
/*
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255, 0, 0) , 50); // red
colorStroboscope(strip.Color(0, 0, 255) , 50);
colorRun(strip.Color( 0, 255, 0) , 500); // green
colorWipe(strip.Color( 0, 0, 255) , 50); // Blue
delay(1000);
colorRun(strip.Color( 255,255,255), 500); // True white (not RGB white)
for(int i=0;i<2;i++){
colorFadeSingleLED(255,0,0);
colorFadeSingleLED(0,255,0);
colorFadeSingleLED(255,255,0);
colorFadeSingleLED(255,0,255);
colorFadeSingleLED(0,255,0);
colorFadeSingleLED(0,255,255);
colorFadeSingleLED(0,0,255);
colorFadeSingleLED(255,255,255);
}
// whiteOverRainbow(75, 5);
// pulseWhite(5);
// rainbowFade2White(3, 3, 1);
*/
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorRun(uint32_t color, int wait) {
for(int j=0; j<strip.numPixels(); j++) { // For each pixel in strip...
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, strip.Color(0,0,0));
if (j==i) {
strip.setPixelColor(i, color);
}
}
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorStroboscope(uint32_t color, int wait) {
for(int j=0; j<10; j++) { // For each pixel in strip...
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color);
}
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, strip.Color(0,0,0));
}
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorFadeSingleLED(uint8_t r, uint8_t g, uint8_t b){
int lednum = (int) random(0, 15);
uint8_t dr = r / 8;
uint8_t dg = g / 8;
uint8_t db = b / 8;
uint8_t r0 = 0;
uint8_t g0 = 0;
uint8_t b0 = 0;
for(int i=0; i<8; i++) {
strip.setPixelColor(lednum, strip.Color(r0,g0,b0));
strip.show();
delay(60);
r0 += dr;
g0 += dg;
b0 += db;
}
delay(500);
for(int i=0; i<8; i++) {
r0 -= dr;
g0 -= dg;
b0 -= db;
strip.setPixelColor(lednum, strip.Color(r0,g0,b0));
strip.show();
delay(60);
}
}
void whiteOverRainbow(int whiteSpeed, int whiteLength) {
if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
int head = whiteLength - 1;
int tail = 0;
int loops = 3;
int loopNum = 0;
uint32_t lastTime = millis();
uint32_t firstPixelHue = 0;
for(;;) { // Repeat forever (or until a 'break' or 'return')
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
if(((i >= tail) && (i <= head)) || // If between head & tail...
((tail > head) && ((i >= tail) || (i <= head)))) {
strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
} else { // else set rainbow
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
}
strip.show(); // Update strip with new contents
// There's no delay here, it just runs full-tilt until the timer and
// counter combination below runs out.
firstPixelHue += 40; // Advance just a little along the color wheel
if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
if(++head >= strip.numPixels()) { // Advance head, wrap around
head = 0;
if(++loopNum >= loops) return;
}
if(++tail >= strip.numPixels()) { // Advance tail, wrap around
tail = 0;
}
lastTime = millis(); // Save time of last movement
}
}
}
void pulseWhite(uint8_t wait) {
for(int j=0; j<256; j++) { // Ramp up from 0 to 255
// Fill entire strip with white at gamma-corrected brightness level 'j':
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
strip.show();
delay(wait);
}
for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
strip.show();
delay(wait);
}
}
void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
int fadeVal=0, fadeMax=100;
// Hue of first pixel runs 'rainbowLoops' complete loops through the color
// wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to rainbowLoops*65536, using steps of 256 so we
// advance around the wheel at a decent clip.
for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the three-argument variant, though the
// second value (saturation) is a constant 255.
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
255 * fadeVal / fadeMax)));
}
strip.show();
delay(wait);
if(firstPixelHue < 65536) { // First loop,
if(fadeVal < fadeMax) fadeVal++; // fade in
} else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
if(fadeVal > 0) fadeVal--; // fade out
} else {
fadeVal = fadeMax; // Interim loop, make sure fade is at max
}
}
for(int k=0; k<whiteLoops; k++) {
for(int j=0; j<256; j++) { // Ramp up 0 to 255
// Fill entire strip with white at gamma-corrected brightness level 'j':
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
strip.show();
}
delay(1000); // Pause 1 second
for(int j=255; j>=0; j--) { // Ramp down 255 to 0
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
strip.show();
}
}
delay(500); // Pause 1/2 second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment