Skip to content

Instantly share code, notes, and snippets.

@eightlines
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eightlines/9766832 to your computer and use it in GitHub Desktop.
Save eightlines/9766832 to your computer and use it in GitHub Desktop.
A modification of ColorCrossFader from the Arduino Playground to work with Adafruit NeoPixels. This script is using a 7 pixel strip. Adjust the #define to suit.
#include <Streaming.h>
#include <Adafruit_NeoPixel.h>
#define BAUD 9600
#define LEDPIN 9
long pMillis = 0;
long interval = 5;
int fadeCallback = 0;
uint8_t
rVal = 0,
gVal = 0,
bVal = 0;
Adafruit_NeoPixel LED = Adafruit_NeoPixel(1, LEDPIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(BAUD);
LED.begin();
LED.setPixelColor(0, LED.Color(rVal, gVal, bVal));
LED.show();
}
void loop() {
switch (fadeCallback) {
case 0: fade(interval, 255, 0, 0, 1); break;
case 1: fade(interval, 0, 255, 0, 2); break;
case 2: fade(interval, 0, 0, 255, 3); break;
}
}
void fade(int duration, int toR, int toG, int toB, int cb) {
for (int i = 256; i > 0; i--) {
unsigned long cMillis = millis();
if (cMillis - pMillis > duration) {
pMillis = cMillis;
rVal += (rVal < toR) ? 1 : (rVal > toR) ? -1 : 0;
gVal += (gVal < toG) ? 1 : (gVal > toG) ? -1 : 0;
bVal += (bVal < toB) ? 1 : (bVal > toB) ? -1 : 0;
if (rVal == toR && gVal == toG && bVal == toB) {
fadeCallback = cb;
} else {
for (int j = 0; j < LED.numPixels(); j++) {
LED.setPixelColor(j, LED.Color(rVal, gVal, bVal));
}
LED.show();
Serial << "R: " << rVal << "|" << toR << ", G: " << gVal << "|" << toG << ", B: " << bVal << "|" << toB << endl;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment