Friday, April 1, 2011

Animatronics Lite

When you are being sick for a week, you really get a tingling in your fingers for coding or tinkering around with a new project. As I was waiting for some new hardware for the next weeks, I decided that I wanted to do something with a servo again. Coming up with something when you only have one servo lying around was pretty hard. I remembered that I kept a stuffed toy gopher in the attic from my childhood days. My girlfriend didn't like the gopher and already asked me why I still keep it. Now I knew.

Rather than throwing the gopher away, I decided to give it some new "life" and mechanical spirit.
At first there is the part of decapitating it to make room for the servo and to allow it to move the head from left to right. Make sure that no children are watching while you do it as this might cause terrible nightmares. Take some of the stuffing out to make the servo fit inside. You might need to attach the servo to the body and to the head so that the head doesn't fall down while it moves.

I connected my servo to an Arduino board and used the circuit and the sketch I already introduced on my blog for controlling a servo. You can find it here. Now I had two buttons to control the head movement of the gopher. I didn't want to just stop there and decided to use the accelerometer in my Nexus S Android phone to control the head movement by tilting the device. You can find an easy example for reading the sensor output in your Android SDK at ANDROID_HOME\samples\android-9\AccelerometerPlay. I shrunk the example to my needs and added some socket communication to address my desktop computer. On my desktop computer I wrote short serial communication class which transfered the current tilt value via serial to the Arduino. I used the RXTX library for serial support in Java. They also provide an example for doing serial communication in Java. The only thing left to do was to listen to the serial port on the Arduino and move the servo according to the tilt value. A simple example for serial communication can be found in the Arduino IDE's examples section. All that stuff can be easily done in an hour or two. When everything is done it looks like this: