[solved]Break out of 'for' statement loop

Hi everyone!

I recently started playing around with a RGB LED module from Sparkfun (http://www.sparkfun.com/products/10504) and i’m using the example code from adafruit (https://github.com/adafruit/WS2801-Library) on an Arduino Pro Mini 3,3, Arduino IDE 1.0.
The code works fine, as i’m not very good at programming i have some issues understanding certain parts, i can’t figure out how to break out of that rainbowCycle function;

void rainbowCycle(uint8_t wait) {
int i, j;

for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
}
strip.show(); // write all the pixels out
delay(wait);
}
}

I just want to end that rainbowCycle and jump to a different part of the program when an input pin is set HIGH.
But not even Interrupts worked, can you guys help me?

Thank you very much!

Frank

Use the break statement.

Inside your inner loop (i), poll your pin for HIGH - when it occurs, set a flag and then "break"; outside of that loop (prior to the strip.show() perhaps), check the flag to see if it is set - if so, "break" again (to get out of the j loop, and return from the function call).

Make sense?

:)

You can use 'break', although when you have nested constructs you need to be careful about the semantics of break in your language.

Alternative you can just define a boolean flag and include it in the for loop condition, set it in the body of the loop when you want to end the loop.

Or, since it seems you want to leave the current function completely (rather than just end the for loop) you could simply call return.

Thank you very much for the fast replies and your suggestions!
I’ve used ‘break’ statements, i also polled the input state at the beginning of the function;
i added another ‘for’ statement because i didn’t manage to insert another comparison to the first
part

for (j=0; j < 256 * 5; j++) {

I also tried switch-case- and if-then-comparisons, everything the reference had to offer.
It seems like once the rainbowCycle is entered he’s doing his thing, not caring for
anything else. Strangely not even interrupts worked.
Later when i’m at home i have to experiment some more, this function literally drives
me nuts…
Polling the input (or flag, i have several of them, coming from the function that actually
handles the input through interrupts) before the ‘strip.show()’ part doesn’t do anything,
sadly. I’ve tried a bunch of different things (like input state polling at every part of the
function, etc.). The funtion just keeps looping inside ‘itself’.
Oh, i almost forgot: the input state polling is handled elsewhere in the program (at the
top, not inside the main loop, from there the function is just called). I switched that
handling to interrupts after experimenting with the rainbowCycle function without
success: The goal was to cycle through the ‘rainbow’ function and when the input state
changes the LEDs shall be filled with a colour (red in my case). With the interrupt
approach i somehow managed to break out of the rainbowCycle function, but the LED
just shows the red colour for about 200 or 300ms, jumping back to the rainbowCycle
immediately.
The example code has also a function which allows one to fill the RGB LEDs
with colours depending on RGB values (via the ‘colorWipe’ function at the bottom of
the example code). That function works well, i can switch through colours through
the digital input so i was sure that i have to find a way to break out of that rainbowCycle
function. Maybe i’m doing something wrong in one of the other parts of the program (though
i stripped it completely down so only the example code and the input handling is left).
So maybe i should post the rest of my program also when i’m back home.

Thank you so much for your help, i’m on the verge of giving up…

Frank

I just want to end that rainbowCycle and jump to a different part of the program when an input pin is set HIGH.
But not even Interrupts worked, can you guys help me?

You can’t break out of a function using interrupts. After the interrupt has finished it will go back to the function it was called from.

It looks like what you want to do is to terminate the outer loop, so something like this:-

void rainbowCycle(uint8_t wait) {
  int i, j;
  
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   if(digitalRead(pin) == LOW) { j = 256 *5; i = strip.numPixels();} // this will finish both loops
    delay(wait);
  }
}

So the extra line:-

if(digitalRead(pin) == LOW) { j = 256 *5; i = strip.numPixels();} // this will finish both loops

Will cause the both loops to end. Note that pin is the input pin you are looking at and it is LOW when pressed. That means connecting the button between ground and the input pin and fitting an external pull up resistor or enabling the internal ones.
Note that the maximum time to respond will depend on the value of:-

delay(wait);

Hi! Now that you've told me, it all makes sense (well, at least the interrupt part): The interrupt routine is executed and after it's finished, the program jumps back to the rainbowCycle loop. That explains why i saw the red colour from my example just a few milliseconds, thanks for the clarification! The additional line you posted looks really promising, when i'm at home later i have to try it. Just one concern left: The digital input comes from a tilt sensor (http://www.sparkfun.com/products/10621 and that's how it works inside: http://www.rohm.com/eu/highlights/product-news/tilt-sensor-rpi-1031.html), so in 'idle state' the sensors 2 outputs are HIGH, in this state the rainbowCycle function should do it's thing. To show and switch through predefined colours the tilt sensor is used so i have a function with counters and comparisons to achieve the desired menu behaviour (showing a predefined colour for each menu entry, you can cycle through them by actuating the tilt sensor until it reaches the end of the menu and switches back to the rainbowCycle). To achieve this i have a variable (tiltcountl) which is zero when in idle mode and gets in- and decremented via interrupts (the sensors outputs (S1 and S2) are connected to pins 2 and 3 of the Arduino, interrupts are triggered by input state CHANGE). Would it be possible to keep all that intact and using it in the line of code you suggested?

if(digitalRead(pin) == LOW) { j = 256 *5; i = strip.numPixels();} // this will finish both loops

would change to

if(tiltcountl >> 0) { j = 256 *5; i = strip.numPixels();} // this will finish both loops

(tiltcountl >> 0 because it never reaches a negative value)

Hi!
I played around with the code a bit, with your suggested line of code i’m now able to exit the rainbowCycle function.
But it doesn’t cycle anymore. The LEDs just show the ‘initial set’ of colours until i actuate the tilt sensor. Switching back to the function is also possible but without the cycling. Breaks and returns do the same. Here’s the line of code i used:
if(digitalRead(2) << 1 || digitalRead(3) << 1)  { j = 256 * 5; i = strip.numPixels();} // this will finish both loops
I also tried a different placement:

void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   delay(wait);  
   }
   if(digitalRead(2) << 1 || digitalRead(3) << 1)  { j = 256 * 5; i = strip.numPixels();} // this will finish both loops
 }

This approach is pretty interesting as it lets the function cycle it’s 5 times and exiting afterwards. Still, when it’s
cycling you pretty much can’t do anything until it finishes it’s 5 loops.

This is how the whole code looks currently, completely stripped down to the (intact) RGB example code and my own interrupt handling
stuff:

#include “SPI.h”
#include “WS2801.h”

int dataPin = 11; //SPI pins for interfacing WS2801
int clockPin = 13;
int RGBspeed = 150; //time each colour is shown
int ledPin = 9; // PWM output to transistor, RGB brightness control

int tiltcountl = 0; //counter left tilts
int tiltcountr = 0; //counter right tilts
boolean lasttiltleft = 0; //store the last tilt directions, left and right
boolean lasttiltright = 0;

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
WS2801 strip = WS2801(4); //I got 4

void setup() {

strip.begin();
// Update LED contents, at start they are all ‘off’
strip.show();
//Serial.begin(9600);

pinMode(2, INPUT); //S1 from tilt sensor
pinMode(3, INPUT); //S2 from tilt sensor

attachInterrupt(0, tiltstate, CHANGE);
attachInterrupt(1, tiltstate, CHANGE);

}

void loop() {

int brightness = 255; //set the brightness of RGB LED

analogWrite(9, brightness); //PWM output

//tiltstate();
//colouroutput();

}

//Handling of the tilt sensor outputs
void tiltstate () {
//tilt1 = S1 and tilt2 = S2 on the tilt-a-whirl board
boolean tilt1 = digitalRead(2);
boolean tilt2 = digitalRead(3);

//clear last time tilted counters when sensor is leveled
if (tilt1 && tilt2 == 1) {
lasttiltright = 0;
lasttiltleft = 0;
}

//jump back to end of wheel when value reaches -1
if (tiltcountl == -1) {
tiltcountl = 5;
}

//clear tilt counter when it reaches value
if (tiltcountl >=6 || tiltcountl <=-6) {
tiltcountl = 0;
}

//tilt counters
//tilt to the left
if (tilt1 == 0 && tilt2 == 1 && lasttiltleft == 0) {
tiltcountl++;
lasttiltleft = 1;
//lasttiltright =0;
}

//tilt to the right
if (tilt2 == 0 && tilt1 ==1 && lasttiltright ==0) {
tiltcountl–;
lasttiltright = 1;
//lasttiltleft =0;
}
colouroutput();
}

//COLOUR OUTPUT
void colouroutput (){

switch (tiltcountl) {
case 1:
colorWipe(Color(255, 0, 0), RGBspeed); //red
break;
case 2:
colorWipe(Color(0, 255, 0), RGBspeed); //green
break;
case 3:
colorWipe(Color(0, 0, 255), RGBspeed); //blue
break;
case 4:
colorWipe(Color(255, 255, 0), RGBspeed); //yellow
break;
case 5:
colorWipe(Color(0, 128, 0), RGBspeed); //dark green
break;
default:
//colorWipe(Color(150, 0, 128), RGBspeed); //dark purple
rainbowCycle(RGBspeed);
}
}

//COLOUR GENERATORS*
//from adafruit example

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
int i, j;

for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
}
strip.show(); // write all the pixels out
delay(wait);
}
if(digitalRead(2) << 1 || digitalRead(3) << 1) { j = 256 * 5; i = strip.numPixels();} // this will finish both loops
}

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

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
if (WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

Why are you using

if(digitalRead(2) << 1

It would be much better to say:-

if(digitalRead(2) == 0

or

if(!digitalRead(2)

the ! operator is pronounced NOT so this is true when the logic input is zero.

It is not clear what your problem is now.
Please use the #icon when posting code not the quote icon next to it.

I’ve used << 1 because i have absolutely no clue what i’m doing, didn’t see a difference, thanks for explaining.
The rainbowCycle function is supposed to ‘cycle’ through the set of 256 colours but now it just shows the first
set of colours and stopping. Although i can exit that function now.

Using << 1 is a logical shift to the left. Have you corrected this now.
Modify your post with the code in to put it in a code box and make the corrections to reflect the code you have now.

Oh my, i got it totally wrong with the ‘<< 1’ statement, i somehow confused it with the ‘=’ and ‘==’ thing. I corrected it before i went
to work this morning but it still doesn’t work. Here’s my whole program code (corrected):

#include "SPI.h"
#include "WS2801.h"

int dataPin = 11;   //SPI pins for interfacing WS2801
int clockPin = 13;
int RGBspeed = 150; //time each colour is shown
int ledPin = 9;    // PWM output to transistor, RGB brightness control

int tiltcountl = 0;  //counter left tilts
int tiltcountr = 0; //counter right tilts
boolean lasttiltleft = 0; //store the last tilt directions, left and right
boolean lasttiltright = 0;

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
WS2801 strip = WS2801(4); //I got 4

void setup() {
    
  strip.begin();
  // Update LED contents, at start they are all 'off'
  strip.show();
  //Serial.begin(9600);
  
  pinMode(2, INPUT); //S1 from tilt sensor
  pinMode(3, INPUT); //S2 from tilt sensor
      
  attachInterrupt(0, tiltstate, CHANGE);
  attachInterrupt(1, tiltstate, CHANGE);
 
}

void loop() {
     
  int brightness = 255;  //set the brightness of RGB LED 
  
  analogWrite(9, brightness);  //PWM output
  
  //tiltstate();
  //colouroutput();

}
 
 //*****Handling of the tilt sensor outputs*****
void tiltstate () {
   //tilt1 = S1 and tilt2 = S2 on the tilt-a-whirl board
   boolean tilt1 = digitalRead(2);
   boolean tilt2 = digitalRead(3);
  
   //clear last time tilted counters when sensor is leveled
   if (tilt1 && tilt2 == 1) {
     lasttiltright = 0; 
     lasttiltleft = 0;
   }
   
    //jump back to end of wheel when value reaches -1
   if (tiltcountl == -1) {
   tiltcountl = 5;
   }
   
   //clear tilt counter when it reaches value
   if (tiltcountl >=6 || tiltcountl <=-6) {
   tiltcountl = 0;
   }
  
   //tilt counters
   //tilt to the left
   if (tilt1 == 0 && tilt2 == 1 && lasttiltleft == 0) {
     tiltcountl++;
     lasttiltleft = 1; 
     //lasttiltright =0;
   }
   
   //tilt to the right
   if (tilt2 == 0 && tilt1 ==1 && lasttiltright ==0) {
     tiltcountl--;
     lasttiltright = 1;
     //lasttiltleft =0;
   }
   colouroutput();
} 

 
 //*****COLOUR OUTPUT*****
 void colouroutput (){
  
  switch (tiltcountl) {
    case 1:
      colorWipe(Color(255, 0, 0), RGBspeed);      //red
      break;
    case 2:
       colorWipe(Color(0, 255, 0), RGBspeed);      //green
      break;   
    case 3:
       colorWipe(Color(0, 0, 255), RGBspeed);      //blue 
      break;
    case 4:
       colorWipe(Color(255, 255, 0), RGBspeed);  //yellow
      break;
    case 5:
       colorWipe(Color(0, 128, 0), RGBspeed);      //dark green
      break;
    default:
       //colorWipe(Color(150, 0, 128), RGBspeed);  //dark purple
       rainbowCycle(RGBspeed);
    }
  }

//***************COLOUR GENERATORS****************
//from adafruit example


// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   delay(wait); 
   }
   if(digitalRead(2) == 0 || digitalRead(3) == 0)  { j = 256 * 5; i = strip.numPixels();} // this will finish both loops
 }

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

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170; 
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

[I totally understand that proper formatting of code in forums is vital although i don’t get why my Arduino IDE (1.0) lets me copy marked code ‘for forum’, using quote tags automatically . I assumed that with ‘forum’ this Arduino forum is meant. Anyway, i used the code tags this time hoping i got it right…]

I am a bit puzzled as to why you say that you can beak out of the routine. You need that line moving, here is just that function:-

//***************COLOUR GENERATORS****************
//from adafruit example
// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   delay(wait); 
   if(digitalRead(2) == 0 || digitalRead(3) == 0)  { j = 256 * 5; } // this will finish the outer loop
   }   
 }

In fact you don’t even need the assignment for i, so I have removed it.
Are you saying that without the terminating line you get the full cycling of colours and with it you don’t?

If i put the line into the function at this place:

//***************COLOUR GENERATORS****************
//from adafruit example
// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   delay(wait); 
   if(digitalRead(2) == 0 || digitalRead(3) == 0)  { j = 256 * 5; } // this will finish the outer loop
   }   
 }

Then the function doesn’t cycle or loop. The following line seems to be only executed once:

strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );

So the LEDs just show the first set of colours, not going up to 256.
If i put the the line at the end of the second loop like this:

//***************COLOUR GENERATORS****************
//from adafruit example


// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
   delay(wait); 
   }
   if(digitalRead(2) == 0 || digitalRead(3) == 0)  { j = 256 * 5; i = strip.numPixels();} // this will finish both loops
 }

Then it is ‘cycling’ (calculating up to 255 exactly 5 times) but during that circulation i can’t break out of the function. I have to wait
until the function has completed the cycling. The second code is from this morning, it still has the

i = strip.numPixels();

in it, i’ll have to check later if removing it will solve the problem.

Then the function doesn't cycle or loop.

In which case the result of

digitalRead(2)

or

digitalRead(3)

must be returning a zero. Check with a separate sketch what these are giving you.

Don't just put that line anywhere think about where you put it. In the last example it is after all the loops so it breaks out of nothing. It only gets executed just before the function is about to end.

Now i removed the i = strip.numPixels(); part with no effect but after uncommenting //colouroutput(); in the main loop the rainbowCycle function is working, cycling indefinitely until one of the inputs turns low. The main loop looks like this now:

void loop() {
     
  int brightness = 255;  //set the brightness of RGB LED 
  
  analogWrite(9, brightness);  //PWM output
  
  //tiltstate();
  colouroutput();

}

I almost started celebrating when i encountered some strange behaviour of the function:
The Arduino is constantly connected to the USB port for power supply and uploading code and after a successful upload, the Arduino gets reset. The program starts, the rainbowCycle function is called and cycles indefinitely like it should, with the speed defined at the top of the code: int RGBspeed = 150; //time each colour is shown When i actuate the tilt sensor (changing one of the two input pins to zero) the loop ends. Everything’s fine until here. But when i jump back to the rainbowCycle function, the first 5 turns (defined in the first for-statement: for (j=0; j < 256 * 5; j++) {    // 5 cycles of all 25 colors in the wheel) are very fast( much faster than the value i defined in RGBspeed). After those 5 turns it gets back to normal, cycling with the defined speed again.
This is the current program code:

#include "SPI.h"
#include "WS2801.h"

int dataPin = 11;   //SPI pins for interfacing WS2801
int clockPin = 13;
int RGBspeed = 150; //time each colour is shown
int ledPin = 9;    // PWM output to transistor, RGB brightness control

int tiltcountl = 0;  //counter left tilts
int tiltcountr = 0; //counter right tilts
boolean lasttiltleft = 0; //store the last tilt directions, left and right
boolean lasttiltright = 0;

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
WS2801 strip = WS2801(4); //I got 4

void setup() {
    
  strip.begin();
  // Update LED contents, at start they are all 'off'
  strip.show();
  //Serial.begin(9600);
  
  pinMode(2, INPUT); //S1 from tilt sensor
  pinMode(3, INPUT); //S2 from tilt sensor
      
  attachInterrupt(0, tiltstate, CHANGE);
  attachInterrupt(1, tiltstate, CHANGE);
 
}

void loop() {
     
  int brightness = 255;  //set the brightness of RGB LED 
  
  analogWrite(9, brightness);  //PWM output
  
  //tiltstate();
  colouroutput();

}
 
 //*****Handling of the tilt sensor outputs*****
void tiltstate () {
   //tilt1 = S1 and tilt2 = S2 on the tilt-a-whirl board
   boolean tilt1 = digitalRead(2);
   boolean tilt2 = digitalRead(3);
  
   //clear last time tilted counters when sensor is leveled
   if (tilt1 && tilt2 == 1) {
     lasttiltright = 0; 
     lasttiltleft = 0;
   }
   
    //jump back to end of wheel when value reaches -1
   if (tiltcountl == -1) {
   tiltcountl = 5;
   }
   
   //clear tilt counter when it reaches value
   if (tiltcountl >=6 || tiltcountl <=-6) {
   tiltcountl = 0;
   }
  
   //tilt counters
   //tilt to the left
   if (tilt1 == 0 && tilt2 == 1 && lasttiltleft == 0) {
     tiltcountl++;
     lasttiltleft = 1; 
     //lasttiltright =0;
   }
   
   //tilt to the right
   if (tilt2 == 0 && tilt1 ==1 && lasttiltright ==0) {
     tiltcountl--;
     lasttiltright = 1;
     //lasttiltleft =0;
   }
   colouroutput();
} 

 
 //*****COLOUR OUTPUT*****
 void colouroutput (){
  
  switch (tiltcountl) {
    case 1:
      colorWipe(Color(255, 0, 0), RGBspeed);      //red
      break;
    case 2:
       colorWipe(Color(0, 255, 0), RGBspeed);      //green
      break;   
    case 3:
       colorWipe(Color(0, 0, 255), RGBspeed);      //blue 
      break;
    case 4:
       colorWipe(Color(255, 255, 0), RGBspeed);  //yellow
      break;
    case 5:
       colorWipe(Color(0, 128, 0), RGBspeed);      //dark green
      break;
    default:
       //colorWipe(Color(150, 0, 128), RGBspeed);  //dark purple
       rainbowCycle(RGBspeed);
       
    }
  }

//***************COLOUR GENERATORS****************
//from adafruit example


// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
  
   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
    delay(wait); 
   if(digitalRead(2) == 0 || digitalRead(3) == 0)  { j = 256 * 5; } // this will finish both loops
    
  
   }
 }

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

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170; 
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

It’s hard to explain and even more confusing and as this isn’t the first program i’ve written (although it may look that way…) it’s extremely frustrating. Unfortunately i’m stuck to that example code from adafruit as i don’t really understand what’s happening in the WS2801.cpp which is part of the library and haven’t worked with SPI and those WS2801 ICs before. I’m totally lost…

Problem solved! I removed the interrupts in the setup.loop:

 attachInterrupt(0, tiltstate, CHANGE);
 attachInterrupt(1, tiltstate, CHANGE);

They were triggered by the same pins (2 and 3) which are used to end the rainbowCycle function. I still can't explain how and why the interrupts caused the rainbowCycle function to run in 'high speed mode' though.

Thank you very much for your help, explanations and patience, especially you, Grumpy_Mike!

Have a nice weekend guys!