Skip to content

Instantly share code, notes, and snippets.

@tekket
Created September 8, 2012 04:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tekket/3671953 to your computer and use it in GitHub Desktop.
Save tekket/3671953 to your computer and use it in GitHub Desktop.
LPD8806 5M Strip Code for Football team colors
#include "LPD8806.h"
#include "SPI.h"
/*****************************************************************************/
// Full 5m LPD8806 STRIP
/*****************************************************************************/
#define holdTime 3000 //used to time button hold for special function
#define buttonPin 23 //Default button pin
//Pin 21 and 22 used for SPI
//To DISABLE Auto USB to save power
const int usb_usb_disable = 0; //disabled usb is enable to save power
int mode = 0; // default mode is off (0)
int nummodes = 8; //number of total modes
long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released
// Number of RGB LEDs in strand:
int nLEDs = 160;
//LPD8806 strip = LPD8806(32, dataPin, clockPin); // In case use different pins.
LPD8806 strip = LPD8806(nLEDs); // Default to SPI pins 21 and 22 on Teensy 2.00
void setup() {
if (usb_usb_disable) Serial.end(); //Disable USB is the usb_usb_disable is 0
Serial.begin(9600); // Set up serial communication at 9600bps to debug via usb
pinMode(buttonPin, INPUT); //Enable BUTTON
// Start up the LED strip
strip.begin(); // Start up the LED strip
// Update the strip, to start they are all 'off'
for (int i = 0; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
strip.show();
}
strip.show();
//Set OUTPUT to pins to save power
for (int i=0; i<21; i++){ pinMode(i, OUTPUT); }
for (int i=24; i<46; i++){ pinMode(i, OUTPUT); }
}
/************************************************************/
// function prototypes, do not remove these!
void colorChase(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void dither(uint32_t c, uint8_t wait);
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
void wave(uint32_t c, int cycles, uint8_t wait);
void rainbowCycle(uint8_t wait);
void police(int);
void failure(int);
void modeSelect();
void returnModeSelect();
uint32_t Wheel(uint16_t WheelPos);
/************************************************************/
/******************************************************************************/
// Main Loop Function //
/******************************************************************************/
void loop() {
//Serial.println(mode); // OFF
if (digitalRead(buttonPin)==LOW){
Serial.println("BUTTON OFF"); // OFF
} delay(50);
if (digitalRead(buttonPin)==HIGH){
Serial.println("BUTTON ON"); // ON
};
if (mode == 0) {
for (int i = 0; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
strip.show();
}
}
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
}
/*****************************************************************************/
modeSelect(); //Defaults to 0 = Off
if (mode > 7) mode = 0;
}
/*************************************************************************/
/******************************************************************************/
// Select Mode Function //
/******************************************************************************/
void modeSelect(){
switch (mode){
case 1:
Serial.println("ColorWipe & Dither");
// Fill the entire strip with...
colorWipe(strip.Color(0,0,127), 10); // blue
dither(strip.Color(105,70,0), 20); // yellow, slow
dither(strip.Color( 0, 0,127), 20); // blue, slow
dither(strip.Color(0,0,0), 5); // black, fast
dither(strip.Color(105,70,0), 20); // yellow, slow
dither(strip.Color(0,0,0), 5); // black, fast
dither(strip.Color( 0, 0,127), 20); // blue, slow
dither(strip.Color(105,70,0), 2); // yellow, slow
break;
case 2:
Serial.println("POLICE");
police(250, 10); // Delay and Repetitions
break;
case 3:
Serial.println("POLICE FAST");
police(100, 10); // Delay and Repetitions
break;
case 4:
Serial.println("Back-and-forth lights");
scanner(105,70,0, 20); // gold, slow
scanner( 0, 0,127, 15); // blue, fast
break;
case 5:
Serial.println("ALL");
colorWipe(strip.Color(0,0,127), 10); // blue
dither(strip.Color(105,70,0), 20); // yellow, slow
dither(strip.Color( 0, 0,127), 20); // blue, slow
dither(strip.Color(0,0,0), 5); // black, fast
dither(strip.Color(105,70,0), 20); // yellow, slow
dither(strip.Color(0,0,0), 5); // black, fast
dither(strip.Color( 0, 0,127), 20); // blue, slow
dither(strip.Color(105,70,0), 2); // yellow, slow
police(250, 15); // Delay and Repetitions
scanner(105,70,0, 20); // gold, slow
scanner( 0, 0,127, 15); // blue, fast
colorWipe(strip.Color(105,70,0), 10); // gold
dither(strip.Color( 0, 0,127), 15); // blue, slow
dither(strip.Color(105,70,0), 20); // yellow, slow
dither(strip.Color(0,0,0), 15); // black, fast
scanner(105,70,0, 15); // gold, slow
scanner( 0, 0,127, 10); // blue, fast
police(250,10);
break;
case 6:
Serial.println("rainbow");
rainbowCycle(0);
break;
case 7:
Serial.println("fail");
failure(250,15);
break;
}
}
/*************************************************************************/
/******************************************************************************/
// SUBFUNCTIONS OF EFFECTS //
/******************************************************************************/
/******************************************************************************/
// Police Flashes //
/******************************************************************************/
void police(int dly, int x) {
for (int r = 0; r < 2; r++){
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for (int i = 0; i < 40; i++){
strip.setPixelColor(i, 105,70,0);
}
for (int i = 80; i < 120; i++){
strip.setPixelColor(i, 105,70,0);
}
strip.show();
delay(dly);
for (int i = 0; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
}
strip.show();
delay(dly);
}
for (int r = 0; r < 2; r++){
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for (int i = 40; i < 80; i++){
strip.setPixelColor(i, 0, 0,127);
}
for (int i = 120; i < 158; i++){
strip.setPixelColor(i, 0, 0,127);
}
strip.show();
delay(dly);
for (int i = 40; i < 80; i++){
strip.setPixelColor(i, 0, 0,0);
}
for (int i = 120; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
}
strip.show();
delay(dly);
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Police Flash. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Color Wipe //
/******************************************************************************/
void colorWipe(uint32_t c, uint8_t wait) {
for (int i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Colorwipe. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Color Chase //
/******************************************************************************/
void colorChase(uint32_t c, uint8_t wait) {
int i;
Serial.println("COLOR CHASE");
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
for (i=0; i < strip.numPixels(); i++) {
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
strip.setPixelColor(i, c); // set one pixel
strip.show(); // refresh strip display
delay(wait); // hold image for a moment
strip.setPixelColor(i, 0); // erase pixel (but don't refresh yet)
}
strip.show(); // for last erased pixel
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Color Chase. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Dither //
/******************************************************************************/
void dither(uint32_t c, uint8_t wait) {
Serial.println("DITHER start");
if (mode == 1 || mode == 5 ) {
// Determine highest bit needed to represent pixel index
int hiBit = 0;
int n = strip.numPixels() - 10;
for(int bit=1; bit < 0x8000; bit <<= 1) {
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
if(n & bit) hiBit = bit;
}
int bit, reverse;
for(int i=0; i<(hiBit << 1); i++) {
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
// Reverse the bits in i to create ordered dither:
reverse = 0;
for(bit=1; bit <= hiBit; bit <<= 1) {
reverse <<= 1;
if(i & bit) reverse |= 1;
}
strip.setPixelColor(reverse, c);
strip.show();
delay(wait);
}
delay(250); // Hold image for 1/4 sec
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Dither. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Larson Scanner //
/******************************************************************************/
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
int i, j, posR, posL, reps, dir, dirR;
// BLUE RGB default
int br=0;
int bg=0;
int bb=127;
// GOLD RGB default
int gr=105;
int gg=70;
int gb=0;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
posL = 80;
posR = 80;
dir = 1;
dirR = -1;
for(i=0; i<159; i++) {
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
// Draw 5 pixels centered on pos. setPixelColor() will clip
// any pixels off the ends of the strip, no worries there.
// we'll make the colors dimmer at the edges for a nice pulse
// look
strip.setPixelColor(posL - 5, strip.Color(gr/4, gg/4, gb/4));
strip.setPixelColor(posL - 4, strip.Color(gr/4, gg/4, gb/4));
strip.setPixelColor(posL - 3, strip.Color(gr/3, gg/3, gb/3));
strip.setPixelColor(posL - 2, strip.Color(gr/2, gg/2, gb/2));
strip.setPixelColor(posL - 1, strip.Color(gr/2, gg/2, gb/2));
strip.setPixelColor(posL, strip.Color(gr, gg, gb));
strip.setPixelColor(posL + 1, strip.Color(br/2, bg/2, bb/2));
strip.setPixelColor(posL + 2, strip.Color(br/2, bg/2, bb/2));
strip.setPixelColor(posL + 3, strip.Color(br/3, bg/3, bb/3));
strip.setPixelColor(posL + 4, strip.Color(br/4, bg/4, bb/4));
strip.setPixelColor(posL + 5, strip.Color(br/4, bg/4, bb/4));
strip.setPixelColor(posR - 5, strip.Color(gr/4, gg/4, gb/4));
strip.setPixelColor(posR - 4, strip.Color(gr/4, gg/4, gb/4));
strip.setPixelColor(posR - 3, strip.Color(gr/3, gg/3, gb/3));
strip.setPixelColor(posR - 2, strip.Color(gr/2, gg/2, gb/2));
strip.setPixelColor(posR - 1, strip.Color(gr/2, gg/2, gb/2));
strip.setPixelColor(posR, strip.Color(br, bg, bb));
strip.setPixelColor(posR + 1, strip.Color(br/2, bg/2, bb/2));
strip.setPixelColor(posR + 2, strip.Color(br/2, bg/2, bb/2));
strip.setPixelColor(posR + 3, strip.Color(br/3, bg/3, bb/3));
strip.setPixelColor(posR + 4, strip.Color(br/4, bg/4, bb/4));
strip.setPixelColor(posR + 5, strip.Color(br/4, bg/4, bb/4));
strip.show();
delay(wait);
// If we wanted to be sneaky we could erase just the tail end
// pixel, but it's much easier just to erase the whole thing
// and draw a new one next time.
for(j=-5; j<= 6; j++) {
strip.setPixelColor(posR+j, strip.Color(0,0,0)); //default to off
strip.setPixelColor(posL+j, strip.Color(0,0,0)); //default to off
}
// Bounce off ends of strip
posL += dir;
if(posL < 0) {
posL = 1;
dir = -dir;
} else if(posL >= strip.numPixels()) {
posL = strip.numPixels() - 2;
dir = -dir;
}
posR += dirR;
if(posR < 0) {
posR = 1;
dirR = -dirR;
} else if(posR >= strip.numPixels()) {
posL = strip.numPixels() - 2;
dirR = -dirR;
}
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Scanner. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Rainbow Cycle //
/******************************************************************************/
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j=0; j < 384 * 3; j++) { // 5 cycles of all 384 colors in the wheel
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 384-color
// wheel (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 384 is to make the wheel cycle around
strip.setPixelColor(i, Wheel(((i * 384 / strip.numPixels()) + j) % 384));
}
strip.show(); // write all the pixels out
delay(wait);
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Rainbow Cycle. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Sine Wave Effect //
/******************************************************************************/
#define PI 3.14159265
void wave(uint32_t c, int cycles, uint8_t wait) {
float y;
byte r, g, b, r2, g2, b2;
// Need to decompose color into its r, g, b elements
b = (c >> 16) & 0x7f;
r = (c >> 8) & 0x7f;
g = c & 0x7f;
for(int x=0; x<(strip.numPixels()*5); x++)
{
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for(int i=0; i<strip.numPixels(); i++) {
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
if(y >= 0.0) {
// Peaks of sine wave are white
y = 1.0 - y; // Translate Y to 0.0 (top) to 1.0 (center)
r2 = 127 - (byte)((float)(127 - r) * y);
g2 = 127 - (byte)((float)(127 - g) * y);
b2 = 127 - (byte)((float)(127 - b) * y);
} else {
// Troughs of sine wave are black
y += 1.0; // Translate Y to 0.0 (bottom) to 1.0 (center)
r2 = (byte)((float)r * y);
g2 = (byte)((float)g * y);
b2 = (byte)((float)b * y);
}
strip.setPixelColor(i, r2, g2, b2);
}
strip.show();
delay(wait);
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Wave. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// Special effect "FAIL" (losing team colors) //
/******************************************************************************/
void failure(int dly, int x) {
Serial.println("fail");
for (int r = 0; r < 2; r++){
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for (int i = 0; i < 40; i++){
strip.setPixelColor(i, 0,127,0);
}
for (int i = 80; i < 120; i++){
strip.setPixelColor(i, 0,127,0);
}
strip.show();
delay(dly);
for (int i = 0; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
}
strip.show();
delay(dly);
}
for (int r = 0; r < 2; r++){
/*****************************************************************************/
if(digitalRead(buttonPin) == HIGH){ //Runs when button is pressed
Serial.println("Button pressed, going to returnModeSelect function");
returnModeSelect(); //Run function to change mode
Serial.println("Finished returnModeSelect function, going to modeSelect");
goto buttonPress; //Skip to end of effect
}
/*****************************************************************************/
for (int i = 40; i < 80; i++){
strip.setPixelColor(i, 0,127,0);
}
for (int i = 120; i < 158; i++){
strip.setPixelColor(i, 0,127,0);
}
strip.show();
delay(dly);
for (int i = 40; i < 80; i++){
strip.setPixelColor(i, 0, 0,0);
}
for (int i = 120; i < 160; i++){
strip.setPixelColor(i, 0, 0,0);
}
strip.show();
delay(dly);
}
/******************** END OF FUNCTION EFFECT *****************************/
buttonPress:
Serial.println("End of effect Fail. Mode is now:");
Serial.println(mode);
return; // End Effect
/******************** END OF FUNCTION EFFECT *****************************/
}
/******************************************************************************/
// HELPER FUNCTIONS //
/******************************************************************************/
/******************************************************************************/
// Wheel Function //
/******************************************************************************/
//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g - b - back to r
uint32_t Wheel(uint16_t WheelPos)
{
byte r, g, b;
switch(WheelPos / 128)
{
case 0:
b = 127 - WheelPos % 128; // blue down
r = WheelPos % 128; // red up
g = 0; // green off
break;
case 1:
r = 127 - WheelPos % 128; // red down
b = WheelPos % 128; // green up
g = 0; // blue off
break;
case 2:
b = 127 - WheelPos % 128; // green down
g = WheelPos % 128; // blue up
r = 0; // red off
break;
}
return(strip.Color(r,g,b));
}
/******************** END OF WHEEL FUNCTION *****************************/
/******************************************************************************/
// Mode Selection Function //
/******************************************************************************/
void returnModeSelect() {
btnDnTime = millis(); //Clock time when pressed initially
Serial.println("btnDnTime is now:");
Serial.println(btnDnTime);
while (digitalRead(buttonPin) == HIGH) { } //do nothing while still pressed
btnUpTime = millis(); //Clock time after button back to off once not pressed
Serial.println("btnUpTime is now:");
Serial.println(btnUpTime);
long time;
time = btnUpTime - btnDnTime;
Serial.println(time);
if (time > holdTime) { //calculate time it was pressed and check if over holdtime
mode=7;
Serial.println("Mode 7 selected");
Serial.println(mode);
} //Go to special mode/case if held long
else {
Serial.println("Alternate mode selected mode++");
mode++; //Go to next mode/case if single press (held short or quick press)
if (mode == 7) mode = 0; //If it reaches the special case function, default to 0
Serial.println("reg mode");
Serial.println(mode);
}
} //End returnModeSelect and to back to effect, then return to bottom of effect.
/******************** END OF FUNCTION ***********************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment