Arduino to Processing


There are 2 ways to use the arduino.
First, you can write in the "Arduino" environment. This programming environment looks a lot like processing and almost all the coding is the same. You would use this if you want to send a program to the arduino, and have the arduino run independently of the computer. Can you say Robot Brain?
Second, you can upload special code called Firmata to the arduino that lets you address the arduio through Processing. After Firmata is on the arduino it's there for good, you only need to upload it once. In this case the arduino will remain connected to the computer via the USB cable.
We are going to concentrate on this second method, but just keep in mind that both are possible for further projects.

Uploading the Firmata software onto the Arduino. YOU ONLY NEED TO DO THIS ONCE!

1. Plug the arduino into the computer with the USB cable. A little LED light should turn on the arduino showing that it has power.
2. Open arduino software, Go file – Sketchbook – Examples – Firmata - StandardFirmata
Of course, as we now know, nothing is as easy as it seems and the lab's version of arduino and Processing are not as up-to-date as they might be. So to open firmata, do a search on the computer for "StandardFirmata", and drag the StandardFirmata.pde file onto the arduino icon in the dock to open it.
3. Go Tools – Board and make sure the right board is selected (if your arduino says "Duemilanove" and it does not show up in the list, I believe you should set it on "Diecimila")
4. Go Tools – Serial Port and select the correct Serial port. It should be /dev/tty.usbserial
5. Go File – Upload to IO Board. Two LED's on the arduino should blink, telling you that it's uploading.

In Processing we are going to open a special "library" called arduino, and another called "serial". We do this in the same area we declare variables. These Libraries includes special code that makes talking to Firmata on the Arduino even easier. Fortunately, we do not need to download these libraries, they are included in the software.
For further reference, here's the site with info about the arduino library, it includes the methods to use to talk to the arduino.

Again, this was not as easy as it should have been. It turns out we need to install the "arduino" library in Processing Library after all. First, you need to create a "libraries" folder in the correct location. from the finder, go to your "home" (the icon with your user name and a little house icon). Then go documents - processing and create a new folder called "libraries" inside the processing folder.
I have downloaded the arduino library and placed it in the class shares folder. (desktop - "Class shares" disk - C178 - something I've forgotten because I'm working at home - arduino). Drag the whole arduino folder (not just the contents of the folder) into the new libraries folder you just created. Are you having fun yet?

Processing to Arduino – "Hello World". Insert the long leg of an LED into pin 13, and the short leg into GND, the whole right next to it. With most pins we would have to add a resistor, but pin 13 is special and has a resistor built in already.


import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;

void setup()
{
//println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(ledPin, Arduino.OUTPUT);// sets port 13 to input
}

void draw()
{
arduino.digitalWrite(ledPin, Arduino.HIGH); // light goes on
delay(1000); // wait 1 second
arduino.digitalWrite(ledPin, Arduino.LOW);// light goes off
delay(1000);
}


If you try this and it does not work, uncomment the line println(Arduino.list()); and run it again. The message area should display a list of the devices connected to the USB ports. If the arduino is in the list, but not at possition 0, change to the appropriate number in the line
arduino = new Arduino(this, Arduino.list()[XX]);
where XX is the new number.

The arduino can be used as both an input and output device, and the input can be both Analog or digital. Next up, digital input (buttons). Push buttons require some circuit building, you need to add a "pull-down" resistor, so the signal is either high or low. DON'T PANIC.
in this picture a breadboard is used to lay out the circuit.

Below is the code to test the push button. Make sure your wire is going into the pin #2.


import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int buttonPin = 2;

void setup(){
size (300, 300);
//println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(buttonPin, Arduino.INPUT);// sets port 2 to input
}

void draw(){
if (arduino.digitalRead(buttonpin) == 0){
background(0);
}
else{
background(255);
}
}


Next, combine the two codes so a light turns on when you push the button!
After that, add the code to pong.