Problems with controlling a LPD8806 led strip over I2c...

Good day all, I am working on a big project with several arduino nano's tied together with I2c.
(and so far it seems to be going well given enough time, but I am still a beginner)

This is the project: I have 1 arduino running as a master on a I2c network, by using buttons it will send the number 1,2,3 etc on the network to the other arduinos. (and showing the function on a display)

Some arduino's will transfer the number to a motor, relais or waveshield or to a LPD8806 led strip.

Now I have got the LPD8806 part to work with the I2C, by sending 1,2 or 3 to this arduino it will execute the light pattern associated with it.
But Iam having trouble keeping the pattern in a loop, it will now run for 1 or 2 times and then I have to send the number again. And the pattern cannot be interrupted.

The funny thing is that the hole LPD8806 script came from a wheelchare project by Cranium.
(many thanks go to him, I've got another LPD8806 project running completly on his script)
Original script: http://arduino.cc/forum/index.php/topic,79839.15.html

The orginal script is using a button who can interrupted the current running loop at all times, but I am not sure how to replace the button control with the I2c control and keep the hole thing running the same.

I will be grateful for any tips or help...
Here is the changed script up till now: (orginal can be found on the link above)

#include "LPD8806.h"
#include "SPI.h"
#include <Wire.h>

#define LED_OUT 13
#define ADDRESS 42


#define button 3
#define stripSize 64
int butPush = 0;
int currentMode = 9;
int numModes = 12;

// on Arduino 168/328 thats data = 11, and clock = pin 13
//LPD8806 strip = LPD8806(stripSize);

// Choose which 2 pins you will use for output.
// Can be any valid output pins.
int dataPin = 5;   
int clockPin = 8; 



// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
LPD8806 strip = LPD8806(10, dataPin, clockPin);


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);
    pinMode(LED_OUT, OUTPUT);
  Serial.begin(9600);
  Wire.begin(ADDRESS);
  Wire.onReceive(receiveEvent);
  
  
}
int micLevel;
int colorLevel;
int numLeds;
int cmd;

void loop() {
  
  }

void receiveEvent(int howMany){
  while (Wire.available() > 0){
    char cmd = Wire.read();
      if (cmd == 0){ // Meaning we need to return the light value from the light sensor
 
  }
  Serial.println(); 

Serial.println(); 


if(cmd == 1){ // Meaning we need to return the light value from the light sensor
 rainbow(25);
      break;
  }
  Serial.println(); 
  
  if(cmd == 2){ // Meaning we need to return the light value from the light sensor
   rainbowCycle(0);  // make it go through the cycle fairly fast
      break;
  }
  Serial.println(); 
  
  if(cmd == 3){ // Meaning we need to return the light value from the light sensor
     // 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;
  }
  Serial.println(); 
  
  if(cmd == 4){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
  
  if(cmd == 5){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
  
  if(cmd == 6){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
  
  if(cmd == 7){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
  
  if(cmd == 8){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
  
  if(cmd == 9){ // Meaning we need to return the light value from the light sensor

  }
  Serial.println(); 
 
 } 


}

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:
      circlinglights(20);  //flash yellow
      break;
      case 8:
      ran(20);  //random white lights
      break;
    case 9:
      ran1(15);  //this is actually the off mode... 
      break;
    case 10:
      shiftwhite(100); //Green with shifting yellow
      break;
    case 11:
      kit(1);
      break;
      case 12:
      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);
    }
  }
}

And the second part of the script:

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,0);  
//      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);
  }
}
void ran1(int dly){  //this is actually the off mode... 
   
       if (butPush == 0) delay(dly);
      if (digitalRead(button)){
        butPush = 1;   //Was the button pushed to change modes?

        }
     


     
       if (butPush == 0) delay(dly);
      if (digitalRead(button)){
        butPush = 1;   //Was the button pushed to change modes?
               }
     }
 

void ran(int dly){
  while (digitalRead(button)==0){
    strip.setPixelColor(random()%stripSize+1,random()%128,random()%128,random()%128);
    strip.show();
    delay(dly);
  }
  butPush = 1;  
}
void circlinglights(int dly){
  int b=0;
  int r=63;
  while (digitalRead(button)==0){
    strip.setPixelColor(b,0,0,2);
    strip.setPixelColor((b+1)%stripSize,0,0,9);
    strip.setPixelColor((b+2)%stripSize,0,0,16);
    strip.setPixelColor((b+3)%stripSize,0,0,40);
    strip.setPixelColor((b+4)%stripSize,0,0,55);
    strip.setPixelColor((b+5)%stripSize,0,0,80);
    strip.setPixelColor((b+6)%stripSize,0,0,127);
    strip.setPixelColor(r,2,0,0);
    strip.setPixelColor((r+stripSize-1)%stripSize,9,0,0);
    strip.setPixelColor((r+stripSize-2)%stripSize,16,0,0);
    strip.setPixelColor((r+stripSize-3)%stripSize,40,0,0);
    strip.setPixelColor((r+stripSize-4)%stripSize,55,0,0);
    strip.setPixelColor((r+stripSize-5)%stripSize,80,0,0);
    strip.setPixelColor((r+stripSize-6)%stripSize,127,0,0);
    strip.show();
    delay(dly);
    strip.setPixelColor(b,0);
    strip.setPixelColor(r,0);
    strip.show();
    if (b++==stripSize) b=0;
    if (r--==0) r=stripSize-1;
  }
  butPush = 1;
}


void music(int all){
  micLevel = ((analogRead(A0)+analogRead(A2)))%1024; //read the pot value and add it to the microphone value to allow color shifting
  if (digitalRead(button)) butPush = 1;   //Was the button pushed to change modes?
  //colorLevel = (micLevel%768)/2;
  colorLevel = micLevel%384; //adjust colorLevel with the color wheel spectrum
  numLeds = map(micLevel,200,600,0,stripSize); //Adjust adjust the most use of LEDs
  
  if (all) numLeds = stripSize;
  for (int i = 0; i < numLeds; i++){
    strip.setPixelColor(i, Wheel((i+colorLevel)%384));
  }  
  delay(map(analogRead(A1),0,1024,0,250));
  strip.show();
    for (int i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0);  // turn all pixels off
    } 
  if (digitalRead(button)) butPush = 1;   //Was the button pushed to change modes?

}

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));
      if (Wire.available() > 1){
        char cmd = Wire.read();   //Was the button pushed to change modes?
        j=384;
      }
    }  
    strip.show();   // write all the pixels out
     if (cmd == 1){
    }
  }
}

// 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 * 5; 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) );
       if (Wire.available() > 2){
        char cmd = Wire.read();   //Was the button pushed to change modes?
        j=384*5;
      }
    }  
    strip.show();   // write all the pixels out
   if (cmd == 2){
   }
  }
}

// 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();
      char cmd = Wire.read();   //Was the button pushed to change modes?
      if (Wire.available() > 3){
          if (cmd == 3){   //Was the button pushed to change modes?
        i = strip.numPixels();
      }
  }
}
}
// 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();
      if (butPush == 0) delay(wait);
      if (digitalRead(button)){
        butPush = 1;   //Was the button pushed to change modes?
        i = strip.numPixels();
      }
  }
}

/* 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));
}

Now trying to get it to work by saving the last know state and repeat the same script until it changes.
Still no luck, maybe this weekend....

And not really closer to a solution just yet, somebody please help? I really need some guidence on this one, (and a new keyboard..) =(

FIxed!!! Yeeeee!! :grin: