Close

Arduino and Processing

I was asked to help build the interactive part of an installation – to be displayed at this week’s WDKA graduation festival.

The installation consists of a super-sized design toy, with a screen showing an animation. The animation changes when people walk in front of it. We decided to build this using Arduino and Processing, if only for the fun of learning new things!

An Arduino board consists of a programmable chip that controls electricity going to, or coming from sensors that you connect to it.

This is a motion sensor connected to the Arduino UNO board. By using the free Arduino software, you can write small programs and upload them to the board.

We wrote a small program that sends out a signal through the USB cable when someone walks in front of the sensor. I was quite amazed by how easy this all is. Just check the few lines of code:

pinMode(2, INPUT);
Serial.begin(9600);

This tells the board to start reading the voltage on port 2 (the yellow wire in the picture). Then it opens a Serial connection to the computer using the USB cable. The next piece of code is executed in a loop and keeps checking if the sensor registers someone walking in front of it:

void loop(){
val = digitalRead(2);
if (val == HIGH) {
Serial.println("1");
}
}

If the value is HIGH, the sensor is sensing movement. This makes the Arduino board send the number “1” through the USB cable.

Reading sensor values with Processing

The next step is to use this input on the computer running the animation. I was thinking about using Flash for animation, but reading a USB port from Flash is quite a hassle, since Flash natively has no access to most of the hardware on your computer.
Processing on the other hand, can read the Serial port directly, using almost the same syntax as Arduino.

// define the serial port
String myPortName = Serial.list()[0];
myPort = new Serial(this, myPortName, 9600);

// read the serial port
while (myPort.available() > 0) {
int inByte = myPort.read();
if(inByte == 49) {
println("received the number 1 from Arduino!");
}
}

The “while” loop is necessary because Serial data arrives one byte at a time. As long as there is data in the pipeline, this loop keeps checking if this is the “1” character we’re sending from the Arduino board (“1” has a byte value of 49).

That’s all you need to get Arduino hardware communicating with software running on your computer!


The finished robot, dubbed “mr. Blinky”.

Read more about Arduino, Processing, and order your own sensors and boards here!