Initial Design:
single Arduino solution to control PureDataOnce distance tests with RS-242/TTL failed we adapted our design to incorporate midi as a transport. Designing board with both midi in and out capabilities proved difficult due to ground loops and lack of functional opto-isolation chips. The revised design called for midi-to-usb conversion at the master pod to alleviate the need for midi-thru loops.

Requiring midi-out brought us to need an Arduino within each pod and with the buttons being out only data source an arduino shield had to be developed to handle all functions of the pod:
- button state detection
- lighting control
- midi-out
Buttons
the 8 buttons per Arduinio gave us plenty of digital input pins. we no longer needed shift registers, and could use a simplified button circuit:
http://www.arduino.cc/en/Tutorial/Button
RGB LEDs
the lighting control came after much experimentation with very bright RGB LEDs. These were broken out to their own board with independent power supply. the Arduino could not drive the high powered LEDs and placement needed to be more flexible. The Arduino controls the RGB values via three PWM pins and a common ground. The values are reversed due to the odd relays we acquired. (ie {R=256,G=256,B=0} is BLUE)
Midi-out

The midi-out circuit is very straight forward with only two connected pins and a dangling ground. These wires are thin and continually come loose and cause trouble.
PCB
Fritzing.org has wonderful layout software that was used to generate the green image above. Soon they'll support eagle export, which will open up the design to any board shop. André Knörig has been an amazing resource by helping with the PCB routing and our submission into the Fritzmas contest.
Code
#include <MIDI.h>
//**************************************************************//
// Name : lightingTemple buttons //
// Author : Matt Pinner //
// Date : 30 Aug, 2009 //
// Version : 1.0 //
// Notes : Code for listening to 8 buttons, //
// : sending midi, //
// : fade RGB LEDs //
//**************************************************************//
// Resource: http://www.arduino.cc //
//****************************************************************
////////////////
// Shift Config
int podNumber = 6;
int debounceDelay = 10; //milli
int buttonCount = 8;
// Color arrays
//int black[3] = { 0, 0, 0 };
//int white[3] = { 100, 100, 100 };
//int red[3] = { 100, 0, 0 };
//int green[3] = { 0, 100, 0 };
//int blue[3] = { 0, 0, 100 };
//int yellow[3] = { 40, 95, 0 };
//int dimWhite[3] = { 30, 30, 30 };
// Color arrays... flipped for our h/w
int black[3] = { 100, 100, 100 };
int white[3] = { 0, 0, 0 };
int red[3] = { 0, 100, 100 };
int green[3] = { 100, 0, 100 };
int blue[3] = { 100, 100, 0 };
int yellow[3] = { 0, 5, 100 };
int purple[3] = { 40, 0, 40 };
int dimWhite[3] = { 70, 70, 70 };
int fadeStart[3] = { 10, 90, 100 };
int fadeEnd[3] = { 100, 90, 10 };
int buttonStartNoteStart = 42;
int buttonNote[] = {2, 3, 4, 5, 6, 7, 8, 9};
int buttonState[] = {0, 0, 0, 0, 0, 0, 0, 0};
int buttonPrevious[] = {0, 0, 0, 0, 0, 0, 0, 0};
int buttonPin[] = {2, 3, 4, 5, 6, 7, 8, 12};
long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0, 0};
//define where your pins are
//int clockPin[] = {4, 12};
//int latchPin[] = {5, 13};
//int dataPin[] = {7, 9};
//int potPin[] = {2, 3};
//int podCount = 1;
/////////
// Midi Config, Local Variable
#define CHANNEL 1
#define LED 13 // LED pin on Arduino board
byte incomingByte;
byte note;
byte velocity;
int action=2; //0 =note off ; 1=note on ; 2= nada
//////////
// Color Config, Local Variables
// Output
int redPin = 9; // Red LED, connected to digital pin 9
int grnPin = 10; // Green LED, connected to digital pin 10
int bluPin = 11; // Blue LED, connected to digital pin 11
int wait = 1; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
int previousWrite = 0;
int stepIndex = 0;
int stepR;
int stepG;
int stepB;
//int buttonColors[8][3] {black, white, red, green, blue, yellow, dimWhite};
//////////
// attract mode
int timeoutMs = 10000;
unsigned long lastActiveMs = 0;
boolean forward = true;
// Set initial color
int redVal = red[0];
int grnVal = red[1];
int bluVal = red[2];
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
//no buttons pressed yet
boolean deactivated = true;
/*****************************************************************************
* Setup pin outs for RGB and MIDI
*/
void setup() {
pinMode(LED, OUTPUT);
// Color Setup
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
for ( int i = 0; i < buttonCount; i++) {
pinMode(buttonPin[i], INPUT);
}
// if (DEBUG) { // If we want to see values for debugging...
// Serial.begin(9600); // ...set up the serial ouput
//} else {
MIDI.begin();
//}
setNextColorSteps(green);
return;
}
/*****************************************************************************
* The primary event loop.
*/
void loop() {
deactivated = true;
for ( int i = 0; i < buttonCount; i++) {
int reading = digitalRead(buttonPin[i]);
// If the switch changed, due to noise or pressing:
if (reading != buttonPrevious[i]) {
// reset the debouncing timer
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
if (reading != buttonState[i]) {
buttonState[i] = reading;
if (LOW == buttonState[i]) {
//Serial.println(i);
//Serial.println("HIGH");
buttonPress(i);
} else{
//Serial.println(i);
//Serial.println("LOW");
buttonRelease(i);
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
buttonPrevious[i] = reading;
if (HIGH == buttonState[i]) {
deactivated = false;
}
} // end for each button
//if (true == deactivated) {
int current = millis();
int inActiveDurationMs = current - lastActiveMs;
if(inActiveDurationMs > timeoutMs) {
// lastActiveMs = current + timeoutMs;
stepIndex++;
// precise loop
//if (stepIndex > 1020) {
if (stepIndex > 1220) {
stepIndex = 0;
// if (previousWrite == 1) {
toggleColor();
//}
}
stepColor(stepIndex, stepR, stepG, stepB);
//}
}
return;
} // end loop()
void toggleColor() {
if (forward) {
forward = false;
//setNextColorSteps(red);
setNextColorSteps(fadeStart);
} else {
forward = true;
//setNextColorSteps(green);
setNextColorSteps(fadeEnd);
resendOff();
}
return;
}
void resendOff() {
for ( int i = 0; i < buttonCount; i++) {
MIDI.send(CC,buttonNote[i],0,podNumber);
}
}
void buttonPress(int i) {
lastActiveMs = millis();
// Serial.println(i); Serial.println("HIGH");
MIDI.send(CC,buttonNote[i],127,podNumber);
writeColor(blue);
return;
}
void buttonRelease(int i) {
lastActiveMs = millis();
// Serial.println(i); Serial.println("LOW");
MIDI.send(CC,buttonNote[i],0,podNumber);
writeColor(red);
return;
}
/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
*
* The program works like this:
* Imagine a crossfade that moves the red LED from 0-10,
* the green from 0-5, and the blue from 10 to 7, in
* ten steps.
* We'd want to count the 10 steps and increase or
* decrease color values in evenly stepped increments.
* Imagine a + indicates raising a value by 1, and a -
* equals lowering it. Our 10 step fade would look like:
*
* 1 2 3 4 5 6 7 8 9 10
* R + + + + + + + + + +
* G + + + + +
* B - - -
*
* The red rises from 0 to 10 in ten steps, the green from
* 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
*
* In the real program, the color percentages are converted to
* 0-255 values, and there are 1020 steps (255*4).
*
* To figure out how big a step there should be between one up- or
* down-tick of one of the LED values, we call calculateStep(),
* which calculates the absolute gap between the start and end values,
* and then divides that gap by 1020 to determine the size of the step
* between adjustments in the value.
*/
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
/* The next function is calculateVal. When the loop value, i,
* reaches the step size appropriate for one of the
* colors, it increases or decreases the value of that color by 1.
* (R, G, and B are each calculated separately.)
*/
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
// Write given values to LED pins
void writeColor(int redVal, int grnVal, int bluVal) {
if ((prevR == redVal)
&& (prevG == grnVal)
&& (prevB == bluVal))
previousWrite++;
analogWrite(redPin, redVal);
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
return;
}
// Write given value to LED pins
void writeColor(int color[3]) {
writeColor(color[0], color[1], color[2]);
return;
}
/* crossFade() converts the percentage colors to a
* 0-255 range, then loops 1020 times, checking to see if
* the value needs to be updated each time, then writing
* the color values to the correct pins.
*/
void crossFade(int color[3]) {
int stepR = calculateStepR(prevR, color);
int stepG = calculateStepG(prevG, color);
int stepB = calculateStepB(prevB, color);
for (int i = 0; i <= 1020; i++) {
stepColor(i, stepR, stepG, stepB);
}
// Update current values for next loop
// prevR = redVal;
//prevG = grnVal;
//prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}
void setNextColorSteps(int nextColor[3]) {
stepR = calculateStepR(prevR, nextColor);
stepG = calculateStepG(prevG, nextColor);
stepB = calculateStepB(prevB, nextColor);
return;
}
int calculateStepR(int prev, int color[3]) {
return calculateStepFromColorPart(prev, color[0]);
}
int calculateStepG(int prev, int color[3]) {
return calculateStepFromColorPart(prev, color[1]);
}
int calculateStepB(int prev, int color[3]) {
return calculateStepFromColorPart(prev, color[2]);
}
int calculateStepFromColorPart(int prev, int color) {
int percent = (color * 255) / 100;
return calculateStep(prev, percent);
}
void stepColor(int stepIndex, int stepR, int stepG, int stepB) {
redVal = calculateVal(stepR, redVal, stepIndex);
grnVal = calculateVal(stepG, grnVal, stepIndex);
bluVal = calculateVal(stepB, bluVal, stepIndex);
// Write current values to LED pins
writeColor(redVal, grnVal, bluVal);
// analogWrite(redPin, redVal);
//analogWrite(grnPin, grnVal);
//analogWrite(bluPin, bluVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
/* if (DEBUG) { // If we want serial output, print it at the
if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
Serial.print("Loop/RGB: #");
Serial.print(i);
Serial.print(" | ");
Serial.print(redVal);
Serial.print(" / ");
Serial.print(grnVal);
Serial.print(" / ");
Serial.println(bluVal);
}
DEBUG += 1;
}*/
}