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