Uploading the Firmata software onto the Arduino. YOU ONLY NEED TO DO THIS ONCE!
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.
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);
}
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.
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);
}
}