Archive for April, 2009

Wiichuck/Arduino controlled LEDs

I got an Arduino a few weeks ago and I’ve been playing with it pretty obsessively since then.   Since the Wii Nunchuck controllers use an I2C serial it’s super easy to connect one to the Arduino.  Here’s a nice tutorial I used to connect the Nunchuck to the Arduino.  I got my WiiChuck adapter from FunGizmos for $4.  It comes unsoldered but it’s only four pins, so no big deal.  I should mention that I’m using Tim Hirzel’s version of the WiiChuck library which has some nice improvements to the rotation data from the Nunchuck; although in this demo I’m not using the accelerometer data.

I’ve also been working for a while on a music visualization project using a bunch of LEDs.  Initially I was connecting the LEDs directly to the PWM outputs on the Arduino.  But since there are only six of them I quickly realized I needed some way to control many more pins.  I found a nice library for the TLC5940 16-channel LED driver from Texas Instruments.  TI offers free samples for many of their components, so I was able to get a couple TLC5940’s for free (no shipping even)!  The library includes all the instructions for how to wire up the device so it was really easy to get that up and going.  The library also offers features specifically for controlling servos which I will have to check out at some point.

The default demo included with the library is a Knight Rider style animation of the LEDs.  I modified that demo to use the joystick input from the nunchuck to move the active LED around.  The input value ranges from -100 to 100 for the joystick which I square and normalize down to get nicer analogish movement (i.e the ability to just slightly move the point or move it very quickly).  I’m using two daisy-chained TLCs, which is almost as easy as using one.  You just take one of the outputs from the first and plug it into the second.  All the rest of the connections can be shared (each does require its own 2k resistor).  One last thing you have to do is adjust the NUM_TLCS variable in tlc_config.h.

Code

#include <Tlc5940.h>
#include <tlc_animations.h>
#include <tlc_config.h>
#include <tlc_fades.h>
#include <tlc_progmem_utils.h>
#include <tlc_servos.h>
#include <math.h>
#include "Wire.h"
#include "WiiChuck.h"

WiiChuck chuck = WiiChuck();
double xJoy;
double position = 15;

void setup() {
Tlc.init();
chuck.begin();
chuck.update();
}

void loop() {
chuck.update();
xJoy = chuck.readJoyX();
//We want to move only if the user has really moved the joystick,
//otherwise the LEDs flicker back and forth.
if (abs(xJoy) > 10) {
if (xJoy < 0) position -= pow(xJoy,2)/5000.0;
if (xJoy > 0) position += pow(xJoy,2)/5000.0;
}
if (position < 1) position = 1;
if (position > 30) position = 30;
Tlc.clear();
Tlc.set((int)position-2, 100);
Tlc.set((int)position-1, 1000);
Tlc.set((int)position, 4095);
Tlc.set((int)position+1, 1000);
Tlc.set((int)position+2, 100);
Tlc.update();
delay(30);

}
Advertisement

Participation

I have been lurking far too long and so I’ve decided to finally participate.  Initially I started creating this blog using Django Basic Apps and while I love how simple and easy-to-implement the  basic blog app is I couldn’t resist the siren song of WordPress so here I am.   The header picture was taken by my friend Katy.

I intend to write primarily about my work and my various projects.  These days I’m spending most of my time working with Python and Django.  Of all the programming languages I’ve worked with Python is by far my favorite and I’m sure there will be plenty of evangelizing about it.

I’ve also been getting into working with the Arduino.  Arduino, if you’re unfamiliar with it is a wonderful open source electronics prototyping platform that uses the Wiring programming language.

Here’s hopin’ I keep up with it!