
Educators: Earn a free Gold upgrade by joining the PBwiki Back To School Challenge.
The automatically created Javadoc documentation for the arduino processing library can be found at: http://hci.stanford.edu/tutorials/arduino/arduino-processing-library/doc
To use Arduino in Processing (assuming the library is installed and you already have Firmata running on your board), you first need to include the right packages at the top of your sketch:
import processing.serial.*; import cc.arduino.*;and create a variable of type Arduino object like so:
Arduino arduino;
Inside your setup() function, you then need to construct a new Arduino object:
arduino = new Arduino(this, Arduino.list()[1], 57600);The first parameter is a reference to your sketch - always use "this". The second parameter is the name of the serial port that your Arduino board is connected to. (Insert: how do I find the right serial port name or index?) The third parameter is the baud rate, or speed, of the serial port. Use 57600 for the current version of Firmata.
arduino.pinMode(pin, Arduino.INPUT); arduino.setPinMode(pin, Arduino.OUTPUT);where pin is an int with value between 2 and 13.
arduino.digitalWrite(pin, Arduino.HIGH); arduino.digitalWrite(pin, Arduino.LOW);
arduino.digitalRead(pin);Returns Arduino.HIGH or Arduino.LOW.
arduino.analogWrite(int pin, int value);where value is an int variable between 0 and 255.
arduino.analogRead(aPin);where aPin is a Number variable with value between 0 and 6. Returns the last known value read from the analog pin: 0 (0 volts) to 1023 (5 volts).
Instead of reading values through analogRead() and digitalRead(), the Arduino library can also use event listener functions you supply whenever a digital or analog pin changes. You just have write a callback function of the right name and signature.
Examples:
/* called whenever an Arduino digital input pin changes value */
void digitalEvent(int pin, int value) {
println("Digital pin "+pin+" has new value "+value);}
/* called whenever an Arduino analog input pin changes value */
void analogEvent(int pin, int value) {
println("Analog pin "+pin+" has new value "+value);
}
To get working examples of how these functions are used, download the example code package: (ArduinoTutorial.zip) and extract the folder into your Processing examples/ directory, then restart Processing.
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |