Friday, October 9, 2009

Creating a Simple Arduino Controller for Processing Sketch

This week I finished my second gadget – a joystick for the bat invaders game that I have been developing in ICM class. The process that I used to create this device can be broken down into the following steps:
  • Define functionality requirements for the joystick
  • Identify components that can meet these requirements
  • Connect and test the components on the Arduino
  • Get Arduino and Processing to work together
  • Update Processing code to work with joystick
Here is a video of the controller in action. Below you will find a more detailed overview of my development process along with details regarding the issues I encountered and how I solved them.

The work related to defining functionality requirements was straightforward and easy. Since the game had already been developed I knew exactly what I needed: two buttons to enable players to shoot bullets and to reset the game, and a controller that enable players to move left and right on the screen. The speaker was added towards the end of the development process hence it was not included in the original requirements definition.

Once these requirements had been defined it was easy to select the appropriate NC (normally closed) push buttons. The solution for controlling the left to right movement of the shooter bat was more challenging. I considered using a potentiometer, force sensors, a joystick switch (made of two potentiometers), and a double throw switch. In the end I opted for the double throw switch due to cost and usability considerations.

Connecting and testing the components was pretty straightforward. I made sure that all core components were working before moving forward with attempting to link Arduino to Processing. To determine which pins from the DTDP (double throw, double pole) switch needed to be connected to the power and input pin on the Arduino I used the continuity test on the multimeter. When I first connected this switch to the Arduino I encountered erratic input readings because I had forgotten to add resistors – this problem was quickly and easily fixed.

I did not test the speaker during this step in the process because I had not decided to add the speaker to the project yet. Once I decided to add the speaker I tested it using a slightly modified version of the code from last week’s tone lab. Then I played around with the tones in order to find two that were sufficiently different to identify bullet shots and bat deaths.

The next step was the toughest part of the entire process, getting Arduino and Processing to communicate with one another. Before starting to work on this project I had already made a failed attempt to use switches on the Arduino to control software developed with processing. With the help of Igal, and ITP colleague, I was finally able to get these two components to communicate analog input and output data (here is a link to a post on Igal’s website where he outlines the steps for making this happen).

Unfortunately, this was not the end of my woes. Though I was able to get analog input and output working I encountered another roadblock when I tried to create an Arduino sketch that would support digital input and output. To solve this issue I hooked up the digital switches to the analog input pins and used the analog values to drive digital behavior - e.g. if the input read higher than 100 then the switch was considered to be in the “on” state; otherwise it was considered to be in “off” state.

When I decided to add speakers as an output device I had to make updates to the Arduino sketch. In order to get this to work I had to take the following steps: (1) add the Tones library to the Arduino sketch; (2) create an object from the Tones class; (3) add code to the analogWriteCallback() function to play the appropriate tones when selected values are passed to the Arduino.

The final integration step was easy now that I had completed all of the pre-work described above – it took me under 30 minutes to get it all working properly.

I’ve posted the Arduino code below for your reference. Please note that the Processing code is available only on the Open Processing website. Also, I’ve commented out all of the elements associated to serial communications since the Open Processing site will not run Java applets that include serial commands.

Arduino Code for Bat Game Controller
/* Supports as many analog inputs and analog PWM outputs as possible.
 * This example code is in the public domain.

// Firmata library allows us to communiacte with Processing, we need to include it

// variable that stores analogPin values
byte analogPin;
long unsigned playStart;
long unsigned interval = 500;
boolean playOn = false;

Tone noiseMaker;    // instance of the tone library

void setup() {

  pinMode(13, OUTPUT);
  // these are calls to setup Firmata for communication
  Firmata.setFirmwareVersion(0, 1);
  // here we attach the analog message funciton, for digital communication
  // we can create other message callback functions and add them here
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  //bitrate, would be a good idea to have it same as in Processing

void loop() {

  // check if Firmata library is loaded
  while(Firmata.available()) {

  // for all the analog pins in Arduino, send the data in each to our Firmata library
  // which we will access from processing
  for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
        Firmata.sendAnalog(analogPin, analogRead(analogPin));


// function that handles all the analog call backs
void analogWriteCallback(byte pin, int value)
    if (value > 3000) {, 5);
    } else {, 25);

No comments: