Create a gist now

Instantly share code, notes, and snippets.

Arduino source code: Color mixing demo for Poughkeepsie Day School Mini Maker Faire
// Color mixing demo for Mini Maker Faire
// Ed Nisley - KE4ANU - November 2016
#include <Adafruit_NeoPixel.h>
//----------
// Pin assignments
#define PIN_NEO 8 // DO - data out to first Neopixel
#define PIN_HEARTBEAT 13 // DO - Arduino LED
#define PIN_FLASH 9 // DI - flash button
#define PIN_POTRED A0 // AI - red potentiometer
#define PIN_POTGREEN A1 // AI - green potentiometer
#define PIN_POTBLUE A2 // AI - blue potentiometer
//----------
// Constants
#define PIXELS 4 // number of pixels
#define PIXEL_RED 2 // physical channel layout
#define PIXEL_GREEN 1
#define PIXEL_BLUE 0
#define PIXEL_MIX (PIXELS - 1) // pixel with mixed color
#define PIXEL_FLASH (PIXELS - 1) // pixel that flashes
// update LEDs only this many ms apart (minus loop() overhead)
#define UPDATEINTERVAL 25ul
#define UPDATEMS (UPDATEINTERVAL - 1ul)
//----------
// Globals
// instantiate the Neopixel buffer array
// color order is RGB for 8 mm diffuse LEDs, GRB for mixed 5050 LED at end
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN_NEO, NEO_RGB + NEO_KHZ800);
uint32_t FullWhite = strip.Color(255,255,255);
uint32_t FullOff = strip.Color(0,0,0);
// colors in each LED
enum pixcolors {RED, GREEN, BLUE, PIXELSIZE};
uint32_t PotColors[PIXELSIZE];
uint32_t UniColor;
unsigned long MillisNow;
unsigned long MillisThen;
//-- Helper routine for printf()
int s_putc(char c, FILE *t) {
Serial.write(c);
}
//------------------
// Set the mood
void setup() {
pinMode(PIN_HEARTBEAT,OUTPUT);
digitalWrite(PIN_HEARTBEAT,LOW); // show we arrived
Serial.begin(57600);
fdevopen(&s_putc,0); // set up serial output for printf()
printf("Color Mixer Demo for Mini Maker Faire\r\nEd Nisley - KE4ZNU - November 2016\r\n");
// set up pixels
strip.begin();
strip.show();
// lamp test: a brilliant white flash on all pixels
// pixel color layout doesn't matter for a white flash
printf("Lamp test: flash white\r\n");
for (byte i=0; i<5 ; i++) {
for (int j=0; j < strip.numPixels(); j++) { // fill LEDs with white
strip.setPixelColor(j,FullWhite);
}
strip.show();
delay(500);
for (int j=0; j < strip.numPixels(); j++) { // fill LEDs with black
strip.setPixelColor(j,FullOff);
}
strip.show();
delay(500);
}
// lamp test: walk a white flash along the string
printf("Lamp test: walking white\r\n");
strip.setPixelColor(0,FullWhite);
strip.show();
delay(500);
for (int i=1; i<strip.numPixels(); i++) {
digitalWrite(PIN_HEARTBEAT,HIGH);
strip.setPixelColor(i-1,FullOff);
strip.setPixelColor(i,FullWhite);
strip.show();
digitalWrite(PIN_HEARTBEAT,LOW);
delay(500);
}
strip.setPixelColor(strip.numPixels() - 1,FullOff);
strip.show();
delay(500);
MillisNow = MillisThen = millis();
}
//------------------
// Run the mood
void loop() {
MillisNow = millis();
if ((MillisNow - MillisThen) >= UPDATEMS) { // time for color change?
digitalWrite(PIN_HEARTBEAT,HIGH);
PotColors[RED] = strip.Color(analogRead(PIN_POTRED) >> 2,0,0);
PotColors[GREEN] = strip.Color(0,analogRead(PIN_POTGREEN) >> 2,0);
PotColors[BLUE] = strip.Color(0,0,analogRead(PIN_POTBLUE) >> 2);
strip.setPixelColor(PIXEL_RED,PotColors[RED]); // load up pot indicators
strip.setPixelColor(PIXEL_GREEN,PotColors[GREEN]);
strip.setPixelColor(PIXEL_BLUE,PotColors[BLUE]);
strip.setPixelColor(PIXEL_MIX,strip.getPixelColor(PIXEL_RED) |
strip.getPixelColor(PIXEL_GREEN) |
strip.getPixelColor(PIXEL_BLUE));
if (PIXEL_FLASH != PIXEL_MIX) {
strip.setPixelColor(PIXEL_FLASH,strip.getPixelColor(PIXEL_MIX));
}
if (LOW == digitalRead(PIN_FLASH)) { // if flash input active, overlay flash
strip.setPixelColor(PIXEL_FLASH,0x00FFFFFF ^ strip.getPixelColor(PIXEL_FLASH));
strip.setPixelColor(PIXEL_RED, 0x00FF0000 ^ strip.getPixelColor(PIXEL_RED));
strip.setPixelColor(PIXEL_GREEN,0x0000FF00 ^ strip.getPixelColor(PIXEL_GREEN));
strip.setPixelColor(PIXEL_BLUE, 0x000000FF ^ strip.getPixelColor(PIXEL_BLUE));
}
UniColor = 0x000000ff & strip.getPixelColor(PIXELS - 1); // hack to rearrange colors for 5050 LED
UniColor |= 0x00ff0000 & (strip.getPixelColor(PIXELS - 1) << 8);
UniColor |= 0x0000ff00 & (strip.getPixelColor(PIXELS - 1) >> 8);
strip.setPixelColor(PIXELS - 1,UniColor);
strip.show(); // send out colors
MillisThen = MillisNow;
digitalWrite(PIN_HEARTBEAT,LOW);
}
}
@ednisley
Owner
ednisley commented Nov 9, 2016

More details on my blog at http://wp.me/poZKh-6ha

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