I've been using lots of repetitive statements to change the colour of some LEDs in time with audio cues from MP3 playback, and am looking for any ways these sections could be cut down.
I'm working with an Uno, a Sparkfun MP3 Player shield (latest version: SparkFun MP3 Player Shield - DEV-12660 - SparkFun Electronics), and an RN-XV attached via a Ciseco Active XBee breakout board. I've got a string of 10 RGB LED pixels incorporating 6803 chips (http://www.earthshineelectronics.com/105-15mm-rgb-led-pixel.html) attached to the board. Everything is working very nicely, but my sketch is seriously pushing up against the size limitations!
Relevant related routine, iterates over LED string and changes colour of each in turn:
void colorWipe(uint16_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) { // numPixels=10
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
My original code looks like:
delay(2200);
colorWipe(Color(63,0,0),50);
delay(300);
colorWipe(Color(0,63,0),50);
delay(300);
colorWipe(Color(0,0,63),50);
delay(200);
colorWipe(Color(63,0,0),50);
delay(700);
colorWipe(Color(0,63,0),50);
delay(300);
colorWipe(Color(0,0,63),50);
delay(200);
colorWipe(Color(63,0,0),50);
delay(200);
colorWipe(Color(0,63,0),50);
delay(700);
colorWipe(Color(0,0,63),50);
delay(300);
colorWipe(Color(63,0,0),50);
delay(200);
colorWipe(Color(0,63,0),50);
delay(100);
colorWipe(Color(0,0,63),50);
delay(700);
...and I've cut this back to:
int delays[] = {2200,300,300,200,700,300,200,200,700,300,200,100,700};
unsigned int pColors[] = {Color(63,0,0),Color(0,63,0),Color(0,0,63)};
byte loopCount = 0;
for(byte i=0;i<13;i++)
{
delay(delays[i]);
colorWipe(pColors[loopCount],50);
if(loopCount<2)
loopCount++;
else
loopCount=0;
}
The integer array in my revised code is a killer and, I suspect, not ideal. Are there any suggestions for improvements?