#include "LPD8806.h"
#include "SPI.h"
#define button 8
#define stripSize 64
int butPush = 0;
int currentMode = 17;
int numModes = 17;
// on Arduino 168/328 thats data = 11, and clock = pin 13
LPD8806 strip = LPD8806(stripSize);
void setup() {
 // Start up the LED strip
 strip.begin();
 // Update the strip, to start they are all 'off'
 strip.show();
//Â Serial.begin(9600);
 pinMode(button,INPUT);
}
int micLevel;
int colorLevel;
int numLeds;
void loop() {
 if (butPush){
  if (currentMode++==numModes) currentMode = 3;
  butPush=0;
  while (digitalRead(button)==1){
   //don't do anything else until the button is released
  }
  for (int i=0; i < strip.numPixels(); i++) {
   strip.setPixelColor(i, 0); // turn all pixels off
  }
  strip.show();
  delay(250);
 }
 modeSelect();
}
void modeSelect(){
 switch (currentMode){
  case 1:
   music(0); //sound sensitive mode with variable LEDs lit
   break;
  case 2:
   music(1); //sound sensitive mode with ALL LEDs lit
   break;
  case 3:
   colorChase(strip.Color(127,0,0), 20); // full brightness red
   colorChase(strip.Color(127,127,0), 20); // orange
   colorChase(strip.Color(0,127,0), 20); // green
   colorChase(strip.Color(0,127,127), 20); // teal
   colorChase(strip.Color(0,0,127), 20); // blue
   colorChase(strip.Color(127,0,127), 20); // violet
   break;
  case 4:
   // fill the entire strip with...
   colorWipe(strip.Color(127,0,0), 40); // red
   colorWipe(strip.Color(0, 127,0), 40); // green
   colorWipe(strip.Color(0,0,127), 40); // blue
   break;
  case 5:
   rainbow(25);
   break;
  case 6:
   rainbowCycle(0); // make it go through the cycle fairly fast
   break;
  case 7:
   caution(600); //flash red and blue
   break;
  case 8:
   police(115); //flash yellow
   break;
  case 9:
   circlinglights(20); //flash yellow
   break;
  case 10:
   allon(); //all white
   break;
  case 11:
   ran(20); //random lights
   break;
  case 12:
   ran1(15); //random lights
   break;
  case 13:
   shiftwhite(100);
   break;
  case 14:
   kit(1);
   break;
  case 15:
   rand2(50);
   break;
  case 16:
   allon1(50); // white flicker
   break;
  case 17:
   xmas(300);
   break;
 }
}
void xmas(int dly){
 int color=0;
 while (butPush==0){
  for (int i=0; i<stripSize; i++){
   if (digitalRead(button)==1) {butPush=1; i=stripSize;}
   if (color==4){
    strip.setPixelColor(i,127,127,127); //white
   }
   else if (color==3){
    strip.setPixelColor(i,127,90,0); //yellow
   }
   else if (color==2){
    strip.setPixelColor(i,0,0,127); //blue
   }
   else if (color==1){
    strip.setPixelColor(i,0,127,0); //green
   }
   else {
    strip.setPixelColor(i,127,0,0); //red
   }
   if (color++==4) color=0;
  }
  if (butPush==0){
   strip.show();
   delay(dly);
  }
 }
}
void allon1(int dly){
 while (digitalRead(button)==0){
  int red = random()%128;
  int green = random()%128;
  int blue = random()%128;
  for (int i = 0; i < stripSize; i++){
  strip.setPixelColor(i, red, green, blue);
 }
 strip.show();
 delay(dly);
  for (int i = 0; i < stripSize; i++){
  strip.setPixelColor(i, 0,0,0);
 }
 strip.show();
 delay(dly);
 }
 butPush = 1;
}
void rand2(int dly){
 //turn on lights one at a time with the same random color
 //ensure that a light is turned on each cycle
 //then turn off the lights 2 at a time
 int lights[stripSize];
 for (int i=0; i<stripSize; i++) lights[i]=0;
 while (digitalRead(button)==0){
  int red = random()%128;
  int green = random()%128;
  int blue = random()%128;
  for (int i = 0; i < stripSize; i++){
   int turnon=0;
   while (turnon==0){
    if (digitalRead(button)==1) {i=stripSize; butPush=1;}
    int x = random()%stripSize;
    if (lights[x]==0){
     turnon=1;
     lights[x]=1;
     strip.setPixelColor(x,red,green,blue);
     strip.show();
    }
   }
  if (butPush==0) delay(dly);
  }
 if (butPush==0) delay(2000);
  for (int i = 0; i < stripSize; i++){
   int turnoff=1;
   while (turnoff==1){
    if (digitalRead(button)==1) {i=stripSize; butPush=1;}
    int x = random()%stripSize;
    if (lights[x]==1){
     turnoff=0;
     lights[x]=0;
     strip.setPixelColor(x,0,0,0);
     strip.show();
    }
   }
  if (butPush==0) delay(dly);
  }
 }
}
void kit(int dly){
 int lmin=0; int lmax=16; int lx=1; int lcur=lmin;
 int rmin=32; int rmax=48; int rx=1; int rcur=rmin;
 int bmin=50; int bmax=62; int bx=0; int bcur=bmax;
 int fmin=18; int fmax=30; int fx=0; int fcur=fmax;
 int wlev=8;
 int lrticks=6; //number of times to loop through before moving the leds
 int fbticks=8; //number of times to loop through before moving the leds
 int tick=1;
 for (int i=lmin; i<lmax; i++){
  strip.setPixelColor(i,wlev,wlev,wlev);
 }
 for (int i=rmin; i<bmax; i++){
  strip.setPixelColor(i,wlev,wlev,wlev);
 }
 while (digitalRead(button)==0){
  for (int i=0; i<2; i++){
   //calculate brightness level for position. Closer to the center is brighter
   //turn on 2 leds at a time
//Â Â Â strip.setPixelColor(lcur+i,127/abs(((lmax-lmin)/2)-lcur),0,0);
//Â Â Â strip.setPixelColor(rcur+i,127/abs(((rmax-rmin)/2)-(rcur-rmin)),0,0);
//Â Â Â strip.setPixelColor(bcur+i,127/abs(((bmax-bmin)/2)-(bcur-bmin)),0,0);
//Â Â Â strip.setPixelColor(fcur+i,127/abs(((fmax-fmin)/2)-(fcur-fmin)),0,0);
   strip.setPixelColor(lcur+i,127,0,0);
   strip.setPixelColor(rcur+i,127,0,0);
   strip.setPixelColor(bcur+i,127,0,0);
   strip.setPixelColor(fcur+i,127,0,0);
  }Â
  strip.show();
  delay(dly);
  strip.show();
  if (tick%lrticks==0){ //Do we need to change the position?
   for (int i=0; i<2; i++){
    //turn the 2 leds back to white
    strip.setPixelColor(lcur+i,wlev,wlev,wlev);
    strip.setPixelColor(rcur+i,wlev,wlev,wlev);
   }Â
   if (lx==1) lcur++; //increment or decrement the position
   else lcur--;
   if (lcur>=lmax) lx=0; //Are we at the max?
   if (lcur<=lmin) lx=1; //Are we at the min?
   if (rx==1) rcur++; //increment or decrement the position
   else rcur--;
   if (rcur>=rmax) rx=0; //Are we at the max?
   if (rcur<=rmin) rx=1; //Are we at the min?
  }
  if (tick%fbticks==0){ //Do we need to change the position?
   for (int i=0; i<2; i++){
    //turn the 2 leds back to white
    strip.setPixelColor(bcur+i,wlev,wlev,wlev);
    strip.setPixelColor(fcur+i,wlev,wlev,wlev);
   }Â
   if (bx==1) bcur++; //increment or decrement the position
   else bcur--;
   if (bcur>=bmax) bx=0; //Are we at the max?
   if (bcur<=bmin) bx=1; //Are we at the min?
   if (fx==1) fcur++; //increment or decrement the position
   else fcur--;
   if (fcur>=fmax) fx=0; //Are we at the max?
   if (fcur<=fmin) fx=1; //Are we at the min?
  }
  if (tick++==lrticks*fbticks) tick=1;
 }
 butPush = 1;
}
void shiftwhite(int dly){
 for (int o = 0; o < stripSize; o++){
  for (int i = 0; i <stripSize; i=i+5){
   strip.setPixelColor((i+o)%stripSize, 127,127,127);Â
//Â Â Â strip.setPixelColor((i+1+o)%stripSize, 127,127,127);Â
   strip.setPixelColor((i+2+o)%stripSize, 0,127,0);Â
   strip.setPixelColor((i+3+o)%stripSize, 0,127,0);Â
   strip.setPixelColor((i+4+o)%stripSize, 0,127,0);Â
   strip.setPixelColor((i+5+o)%stripSize, 0,127,0);Â
   if (digitalRead(button)){
    butPush = 1; //Was the button pushed to change modes?
    i = stripSize;
    o = stripSize;
    }
  }
  strip.show();
  if (butPush == 0) delay(dly);
 }
}