See also CreaproJavadoc for a more formal documentation.

1. Arduino in Processing

Together with the IDuino firmware (an Arduino sketch uploaded to the Arduinoboard), an object of the Arduino class acts as a proxy to the Arduino board, allows to control the Arduino board from Processing: reading from and writing to the digital pins and reading the analog inputs, reacting on the input data when available.

Note that the core elements of the Arduino class is a Java copy or simulation of their counterparts in the Arduino language. The other elements are designed for communication and configuration purposes.

1.1. Static constants

INPUT
Constant to set a pin to input mode (in a call to pinMode()).
OUTPUT
Constant to set a pin to output mode (in a call to pinMode()).
LOW
Constant to write a low value (0 volts) to a pin (in a call to digitalWrite()).
HIGH
Constant to write a high value (+5 volts) to a pin (in a call to digitalWrite()).

1.2. Functions

1.2.1. Constructors

Arduino(parent, name)
Create a proxy to an Arduino board running the IDuino firmware. it uses a default baud rate 57600 to communicate with the Arduino board.
parent

the Processing sketch creating this Arduino board (i.e. this).

name
the name of the serial device associated with the Arduino board (e.g. one the elements of the array returned by Arduino.list())
Arduino(parent, name, rate)
Create a proxy to an Arduino board running the IDuino firmware.
parent

the Processing sketch creating this Arduino board (i.e. this).

name
the name of the serial device associated with the Arduino board (e.g. one the elements of the array returned by Arduino.list())
rate
the baud rate to use to communicate with the Arduino board.

1.2.2. Configurations

String [ ] Arduino.list()
Get a list of the available Arduino boards; currently all serial devices (i.e. the same as Serial.list()).
enableAnalogInput(int pin)
Enables a particular analog input pin, otherwise no input from this pin will be reported to the host computer.
pin
the analog pin whose value should be returned (from 0 to 5).
setAnalogPullInterval(int interval)
Sets the analog pull interval (ms). IDuino firmware tries to check the values from the analog input pins every 20 ms (by default). This interval can be changed by setting a different pull interval, especially when it is necessary to lower the communication load to the host computer. Any value that is lower than 20ms is not recommended.
interval
the new analog pull interval in milliseconds.
setAnalogStep(int step)
Sets the setup of the analog input value. The values from the analog pins varies from 0 to 1023, and any slight changes will be communicated to the host computer. It may result in high communication load when the analog input constantly changes. The communication load can be lowered by reducing the sensibility -- only the changes that are bigger than a given step will be reported.
step
the new analog step.
setSerialPullInterval(int interval)
Sets the serial pull interval (ms). IDuino firmware tries to check the values from the serial port every 20 ms (by default). This interval can be changed by setting a different pull interval, especially when it is not necessary to check the communication as often as every 20ms. Any value that is lower than 20ms is not recommended.
interval
the new serial pull interval in milliseconds.
pinMode(int pin, int mode)
set a digital pin to input or output mode (Arduino.INPUT or Arduino.OUTPUT).
pin
the pin whose mode to set (from 2 to 13).
mode
either Arduino.INPUT or Arduino.OUTPUT.

1.2.3. Read and Write operations

int digitalRead(int pin)
returns the value of a digital pin, either Arduino.LOW or Arduino.HIGH (the pin must be set as an input).
pin
the digital pin whose value should be returned (from 2 to 13, since pins 0 and 1 are used for serial communication).
digitalWrite(int pin, int value)
writes Arduino.LOW or Arduino.HIGH to a digital pin.
pin
the pin to write to (from 2 to 13)
int analogRead(pin)
Reads the last known value read from the analog pin: 0 (0 volts) to 1023 (5 volts).
pin
the analog pin whose value should be returned (from 0 to 5).
analogWrite(pin, value)
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite, the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin). The frequency of the PWM signal is approximately 490 Hz. On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
pin
the pin to write to.
value
the duty cycle: between 0 (always off) and 255 (always on).

1.3. Example

   1 import processing.serial.*;
   2 import nl.tue.id.creapro.*;
   3 
   4 Arduino arduino;
   5 int ledPin = 13;
   6 
   7 void setup()
   8 {
   9   println(Arduino.list());
  10   arduino = new Arduino(this, "COM3"); // v2
  11   //arduino = new Arduino(this, Arduino.list()[0], 57600); // v1
  12   delay(2000);
  13   arduino.pinMode(ledPin, Arduino.OUTPUT);
  14   delay(50);
  15   arduino.pinMode(11, Arduino.INPUT);
  16   delay(50);
  17   arduino.analogEnable(0);
  18   delay(50);
  19   arduino.setAnalogStep(4);
  20   delay(50);
  21   arduino.setAnalogPullRate(500);
  22   delay(50);
  23   println("Starting...");
  24 //  arduino.clearCommandCount();
  25  (new Repeat()).start();
  26  delay(100);
  27 
  28   noLoop();
  29 }
  30 
  31 void draw()
  32 {
  33 }
  34 
  35 void digitalAvailable11(int o, int n){
  36   println(n);
  37 }
  38 
  39 void analogAvailable0(int o, int n){
  40   println(n);
  41 }
  42 
  43 class Repeat extends Thread{
  44   public void run(){
  45     arduino.clearCommandCount();
  46       for (int i=0; i<8; i++){
  47   arduino.digitalWrite(ledPin, Arduino.HIGH);
  48   try{sleep(400);}catch(Exception e){}
  49   arduino.digitalWrite(ledPin, Arduino.LOW);
  50   try{sleep(400);}catch(Exception e){}  }
  51   try{sleep(2000);}catch(Exception e){}
  52     arduino.reportCommandCount();
  53     arduino.reportCommandCount();
  54     arduino.reportCommandCount();
  55     arduino.reportCommandCount();
  56   }
  57 
  58 }