Not sure what I'm doing wrong here but here's the code I have so far. Can't get it to compile.
Code: Select all
#include "LPD8806.h"
#include "SPI.h"
int dataPin = 2;
int clockPin = 3;
const int numPixels = 160;
LPD8806 strip = LPD8806(160, dataPin, clockPin);
void setup() {
strip.begin();
strip.show();
Serial.begin(9600);
}
void loop() {
// read the sensor:
if (Serial.available() > 0) {
char inByte = Serial.read();
Serial.print("Command Received ");
Serial.print(inByte);
Serial.print("\n\r");
int i;
uint32_t c;
switch (inByte) {
case 'a':
while (!Serial.available()) {
// Set all pixels to red
c = strip.Color(255,0,0);
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
//delay(100);
}
break;
case 'b':
while (!Serial.available()) {
//green to red wave
wave(strip.Color(0,127,0),60,0,100); //color, cycles, delay, repetitions
wave(strip.Color(20,107,0),50,0,50); //color, cycles, delay, repetitions
wave(strip.Color(40,87,0),40,0,50); //color, cycles, delay, repetitions
wave(strip.Color(80,47,0),30,0,50); //color, cycles, delay, repetitions
wave(strip.Color(107,27,0),20,0,100); //color, cycles, delay, repetitions
wave(strip.Color(127,0,0),5,0,200); //color, cycles, delay, repetitions
}
break;
case 'c':
while (!Serial.available()) {
twoColorSparkle(strip.Color(127,0,0), strip.Color(0,127,0), 50, 10); //color 1, color 2, density, delay - random sparkles in two colors
}
break;
case 'd':
while (!Serial.available()) {
rainbowCycle(0); // make it go through the cycle fairly fast
}
break;
case 'e':
while (!Serial.available()) {
turnAllOff();
}
break;
case 'f':
while (!Serial.available()) {
chaseMultipleLinesLToH(10,10,0,152); //number of leds in line, delay, low,
}
break;
case 'g':
while (!Serial.available()) {
turnAllOn(strip.Color(0,0,80),200);
dither(strip.Color(127,0,0), 10); //color, delay - random fills up the strip
dither(strip.Color(0,0,80), 10); //color, delay - random fills up the strip
//fadeUp(0,0,127,30); //red, green, blue, delay - fade up all pixels one color
}
break;
case 'h':
while (!Serial.available()) {
randomSparkleUpSegment(strip.Color(0,127,0),15,10,0,100); //color, density, delay, low point, high point
turnSegmentOn(strip.Color(0,127,0),200,0,100); //color, delay, start of segment, end of segment
delay(500);
}
break;
case 'i':
while (!Serial.available()) {
fillUpTheStar(strip.Color(127,0,0),30); //color, delay
fillUpCenterStar(strip.Color(0,127,0), 30); //color, delay
fadeDown(127,0,0,30);
}
break;
case 'j':
while (!Serial.available()) {
colorOverColorChase(strip.Color(127, 0, 0), 100); // Red
colorOverColorChase(strip.Color( 0,127, 0), 100); // Green
colorOverColorChase(strip.Color( 0, 0,127), 100); // Blue
colorOverColorChase(strip.Color(127,127,127), 100); // White
}
break;
}
}
}
// An "ordered dither" fills every pixel in a sequence that looks
// sparkly and almost random, but actually follows a specific order.
void dither(uint32_t c, uint8_t wait) {
// Determine highest bit needed to represent pixel index
int hiBit = 0;
int n = strip.numPixels() - 1;
for(int bit=1; bit < 0x8000; bit <<= 1) {
if(n & bit) hiBit = bit;
}
int bit, reverse;
for(int i=0; i<(hiBit << 1); i++) {
// 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
}
// "Larson scanner" = Cylon/KITT bouncing light effect
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
int i, j, pos, dir;
pos = 0;
dir = 1;
for(i=0; i<((strip.numPixels()-1) * 8); i++) {
// 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(pos - 2, strip.Color(r/4, g/4, b/4));
strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
strip.setPixelColor(pos, strip.Color(r, g, b));
strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/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=-2; j<= 2; j++)
strip.setPixelColor(pos+j, strip.Color(0,0,0));
// Bounce off ends of strip
pos += dir;
if(pos < 0) {
pos = 1;
dir = -dir;
} else if(pos >= strip.numPixels()) {
pos = strip.numPixels() - 2;
dir = -dir;
}
}
}
// Sine wave effect
#define PI 3.14159265
void wave(uint32_t c, int cycles, uint8_t wait, int repetitions) {
float y;
byte r, g, b, r2, g2, b2;
// Need to decompose color into its r, g, b elements
g = (c >> 16) & 0x7f;
r = (c >> 8) & 0x7f;
b = c & 0x7f;
for(int x=0; x<repetitions; x++)
{
for(int i=0; i<strip.numPixels(); i++) {
y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
y = 1.0 - y; // Translate Y to 0.0 (top) 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);
}
}
void chaseUpTheStar(uint32_t c, uint8_t wait) {
int i,j;
strip.setPixelColor(80, c);
delay(wait);
strip.setPixelColor(80, strip.Color(0,0,0));
for (i = 1; i <= 20; i++) {
strip.setPixelColor(80+i, c);
strip.setPixelColor(80-i, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(80+i, strip.Color(0,0,0));
strip.setPixelColor(80-i, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
for (j=0; j<=29; j++) {
strip.setPixelColor(j, c);
strip.setPixelColor(59-j, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(j, strip.Color(0,0,0));
strip.setPixelColor(59-j, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
}
void chaseUpCenterStar(uint32_t c, uint8_t wait) {
int i,j;
for (i = 1; i <= 6; i++) {
strip.setPixelColor(146+i, c);
strip.setPixelColor(147-i, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(146+i, strip.Color(0,0,0));
strip.setPixelColor(147-i, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
for (j=0; j<=18; j++) {
strip.setPixelColor(103+j, c);
strip.setPixelColor(140-j, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(103+j, strip.Color(0,0,0));
strip.setPixelColor(140-j, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
}
void fillUpTheStar(uint32_t c, uint8_t wait) {
int i,j;
strip.setPixelColor(80, c);
delay(wait);
for (i = 1; i <= 20; i++) {
strip.setPixelColor(80+i, c);
strip.setPixelColor(80-i, c);
strip.show(); // write all the pixels out
delay(wait);
}
for (j=0; j<=29; j++) {
strip.setPixelColor(j, c);
strip.setPixelColor(59-j, c);
strip.show(); // write all the pixels out
delay(wait);
}
}
void fillUpCenterStar(uint32_t c, uint8_t wait) {
int i,j;
for (i = 1; i <= 6; i++) {
strip.setPixelColor(146+i, c);
strip.setPixelColor(147-i, c);
strip.show(); // write all the pixels out
delay(wait);
}
for (j=0; j<=18; j++) {
strip.setPixelColor(103+j, c);
strip.setPixelColor(140-j, c);
strip.show(); // write all the pixels out
delay(wait);
}
}
void chaseBetweenLToH(uint32_t c, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i;
for (i = pointL; i <= pointH; i++) {
strip.setPixelColor(i, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(i, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
}
void chaseBetweenHToL(uint32_t c, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i;
for (i = pointH; i >= pointL; i--) {
strip.setPixelColor(i, c);
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(i, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
}
void chaseLineLToH(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i, j;
for(i = pointL; i < pointH+lineNumber; i++) {
for(j = 0; j < lineNumber; j++) {
strip.setPixelColor(i-j, c);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
}
}
void chaseLineHToL(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i, j;
for(i = pointH; i > pointL; i--) {
for(j = 0; j<lineNumber; j++){
strip.setPixelColor(i+j, c);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
}
}
void chaseToCenter(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i, j, k;
i = pointL;
k = pointH;
while(i < k && k > i) {
for(j = 0; j < lineNumber; j++) {
strip.setPixelColor(i-j, c);
strip.setPixelColor(k+j, c);
}
strip.show(); // write all the pixels out
delay(wait);
i++;
k--;
turnAllOff();
}
}
void chasePastCenter(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i, j, k;
i = pointL;
k = pointH;
while(i < pointH && k > pointL) {
for(j = 0; j < lineNumber; j++) {
strip.setPixelColor(i-j, c);
strip.setPixelColor(k+j, c);
}
strip.show(); // write all the pixels out
delay(wait);
i++;
k--;
turnAllOff();
}
}
void turnAllOn(uint32_t c, uint32_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c); // turn all pixels on
}
strip.show(); // write all the pixels out
delay(wait);
}
void turnSegmentOn(uint32_t c, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
int i;
for (i=startofseg; i <= endofseg; i++) {
strip.setPixelColor(i, c); // turn all pixels on
}
strip.show(); // write all the pixels out
delay(wait);
}
void fadeUpSeg(uint32_t r, uint32_t g, uint32_t b, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
int i, j;
for (j=0; j <= 100; j++) {
for (i=startofseg; i<=endofseg; i++) {
strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));
}
strip.show();
}
delay(wait);
}
void fadeDownSeg(uint32_t r, uint32_t g, uint32_t b, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
int i, j;
for (j=100; j >= 0; j--) {
for (i=startofseg; i<=endofseg; i++) {
strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100)); // turn all pixels on
}
strip.show();
}
delay(wait);
}
void fadeUp(uint32_t r, uint32_t g, uint32_t b, uint32_t wait) {
int i, j;
for (j=0; j <= 100; j++) {
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));
}
strip.show();
}
delay(wait);
}
void fadeDown(uint32_t r, uint32_t g, uint32_t b, uint32_t wait) {
int i, j;
for (j=100; j >= 0; j--) {
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100)); // turn all pixels on
}
strip.show();
}
delay(wait);
}
void chaseMultipleLinesLToH(uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i, j;
for(i = pointL; i <= pointH; i++) {
for(j = 0; j < lineNumber; j++) {
strip.setPixelColor(i, strip.Color(0,0,127));
strip.setPixelColor(i+j+(2*lineNumber), strip.Color(127,0,0));
strip.setPixelColor(i+j+(3*lineNumber), strip.Color(0,127,0));
strip.setPixelColor(i+j+(4*lineNumber), strip.Color(127,127,0));
strip.setPixelColor(i+j+(5*lineNumber), strip.Color(0,127,127));
strip.setPixelColor(i+j+(6*lineNumber), strip.Color(127,0,127));
strip.setPixelColor(i+j+(7*lineNumber), strip.Color(0,0,127));
strip.setPixelColor(i+j+(8*lineNumber), strip.Color(127,0,0));
strip.setPixelColor(i+j+(9*lineNumber), strip.Color(0,0,0));
}
strip.show(); // write all the pixels out
delay(wait);
}
}
void turnAllOff() {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
strip.show(); // write all the pixels out
}
void randomSparkleUpSegment(uint32_t c, uint32_t density, uint8_t wait, uint8_t pointL, uint8_t pointH) {
int i,j,location;
for (j=pointL; j<=pointH; j++) {
for (i=0; i <= density; i++) {
location = random(pointL,j);
strip.setPixelColor(location, c);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
strip.show(); // write all the pixels out
}
}
void randomSparkleUpStrip(uint32_t c, uint32_t density, uint8_t wait) {
int i,j,location;
for (j=density; j<strip.numPixels(); j++) {
for (i=0; i <= density; i++) {
location = random(0,j);
strip.setPixelColor(location, c);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
strip.show(); // write all the pixels out
}
}
void randomSparkle(uint32_t c, uint32_t density, uint8_t wait) {
int i,j,location;
for (j=0; j < 200; j++) {
for (i=0; i <= density; i++) {
location = random(0,strip.numPixels()); //random location in first 5th of strip
strip.setPixelColor(location, c);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
strip.show(); // write all the pixels out
}
}
void randomColorSparkle(uint8_t wait) {
int i;
int location1, location2, location3, location4, location5;
int color1, color2, color3;
for (i=0; i < 200; i++) {
location1 = random(0,32); //random location in first 5th of strip
location2 = random(33,64); //random location in 2nd 5th of strip
location3 = random(65,96); //random location in 3rd 5th of strip
location4 = random(97,128); //random location in 4th 5th of strip
location5 = random(129,159); //randon location in last 5th of strip
color1 = random(127);
color2 = random(127);
color3 = random(127);
strip.setPixelColor(location1, strip.Color(color1,color2,0));
strip.setPixelColor(location2, strip.Color(color3,0,color1));
strip.setPixelColor(location3, strip.Color(0,color2,color3));
strip.setPixelColor(location4, strip.Color(color1,color2,0));
strip.setPixelColor(location5, strip.Color(color3,0,color1));
strip.show(); // write all the pixels out
delay(wait);
strip.setPixelColor(location1, strip.Color(0,0,0));
strip.setPixelColor(location2, strip.Color(0,0,0));
strip.setPixelColor(location3, strip.Color(0,0,0));
strip.setPixelColor(location4, strip.Color(0,0,0));
strip.setPixelColor(location5, strip.Color(0,0,0));
strip.show(); // write all the pixels out
}
}
void twoColorSparkle(uint32_t c1, uint32_t c2, uint32_t density, uint8_t wait) {
int i,j,location1,location2;
for (j=0; j < 1; j++) {
for (i=0; i <= density; i++) {
location1 = random(0,strip.numPixels()); //random location number 1
location2 = random(0,strip.numPixels()); //random location number 2
strip.setPixelColor(location1, c1);
strip.setPixelColor(location2, c2);
}
strip.show(); // write all the pixels out
delay(wait);
turnAllOff();
strip.show(); // write all the pixels out
}
}
void rainbow(uint8_t wait) {
int i, j;
for (j=0; j < 384; j++) { // 3 cycles of all 384 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 384));
}
strip.show(); // write all the pixels out
delay(wait);
}
}
// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j=0; j < 384 * 1; j++) { // 5 cycles of all 384 colors in the wheel
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);
}
}
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Chase a dot down the strip
// good for testing purposes
void colorChase(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
if (i == 0) {
strip.setPixelColor(strip.numPixels()-1, 0);
} else {
strip.setPixelColor(i-1, 0);
}
strip.show();
delay(wait);
}
}
void colorOverColorChase(uint32_t color, uint32_t backgroundColor, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
if (i == 0) {
strip.setPixelColor(strip.numPixels()-1, backgroundColor);
} else {
strip.setPixelColor(i-1, backgroundColor);
}
strip.show();
delay(wait);
}
}
/* Helper functions */
//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:
r = 127 - WheelPos % 128; //Red down
g = WheelPos % 128; // Green up
b = 0; //blue off
break;
case 1:
g = 127 - WheelPos % 128; //green down
b = WheelPos % 128; //blue up
r = 0; //red off
break;
case 2:
b = 127 - WheelPos % 128; //blue down
r = WheelPos % 128; //red up
g = 0; //green off
break;
}
return(strip.Color(r,g,b));
}