I watched a lot of The Colony over winter break, and I was inspired by my favorite colony member, John C., to make rgb LED headwear. I had rgb LEDs left over from a previous project that was on hiatus and a sombrero from an elementary school project, so I decided to combine the two together. This was the result:
Two New Modes from sambot on Vimeo.
The overall idea was to have an Arduino power and control the LEDs and have a computer create logic for interesting patterns. The first step was wiring up the LEDs to the Arduino. Below are some pictures of this process:
Unlike the previous project that used these LEDs, I was only controlling LEDs (no buttons this time), so I had all of the Arduino digital outputs to work with. However, having 16 rgb LEDs required controlling 48 outputs (16 LEDs x 3 colors) and the Arduino only has 14 digital outputs. I was able to get around this by performing two 'tricks'.
The first trick was exploiting the lesser-known feature of the arduino, that is, using the analog pins as digital outputs. This can easily done in code by defining pins 14-19 (analog pins 0-5, respectively) as outputs using pinMode().
The second trick was to wire the LEDs in groups. This is where things get a bit complicated. Once the LEDs were installed into the hat, I divided them into four groups of four LEDs and connected the red, green, and blue together, separately. By that, I mean that in a given group, all of the red LEDs were connected to each other, the blue LEDs were connect to the other blue LEDs, etc. Once this was done, I connected the first ground pin from the first group to the first ground pins in the other group, the second ground pin from the first group to the other second ground pins, and so on. Since the colors in each group were all connected, yet within a group, the grounds were separated, I could select a single LED at once by 'choosing' which ground I wanted and which color I wanted. This 'choosing' involved setting whichever ground I wanted as LOW and the rest as HIGH, and setting the color pin I wanted as HIGH and the rest as LOW. Since the LEDs are diodes, I was able to keep the LEDs I didn't want on by reversing the current through them.
To test this, I wrote a 'hello world' Arduino sketch that flashed each color of each LED in order. Here's a video of it in action:
LED Sombrero 'Hello World' from sambot on Vimeo.
The video at the top of this article demonstrates two additional modes that I wrote: "Rainbow Chase" and "Fiesta", respectively. Once all of this backend work was complete, I could finally focus on my ideal usage for the hat: flashing to music!
Surprisingly, this was much simpler than I had thought it would be. From playing around with minim and FFTs in the past, I knew that I could use Processing to identify the beat of a song. Rather than have Processing play a song, I used Soundflower to reroute the audio played by iTunes to be read as the audio input on my computer. This allowed me to use playlists and go through my music library per usual and have Processing perform a beat detection in the background.
The final piece to write was a communication scheme between Processing and the Arduino in the hat. My initial thought was to use Firmata to control the Arduino, however, I had problems with the associated Processing library (internet research leads me to believe that it wasn't compiled for 64 bit Java). Since I only needed to communicate a few values (LED index and color), instead, I created my own communication protocol. Over the serial line, the Arduino would continually watch for an integer index for the LED and a character representing which color (see the code).
Below is a quick example of the beat detection running. The red LED flashes if a high-hat is detected, the green LED flashes if a kick drum is detected, and the blue LED flashes if a snare drum is detected. The beat detector works reasonably well (there are some syncing issues), however, its performance varies depending on the song.
IMG 0194 from sambot on Vimeo.
Below is the Arduino and Processing code for the project. To choose which mode to run, uncomment the desired mode (beatPulse(), rainbowChase(), fiesta(), or rgbTest()) in the loop() function of the Arduino code.