nl.tue.id.creapro.arduino
Class Arduino

java.lang.Object
  extended by nl.tue.id.creapro.arduino.Arduino
All Implemented Interfaces:
ArduinoConstants, SerialConstants

public class Arduino
extends java.lang.Object
implements ArduinoConstants, SerialConstants

Together with the IDuino firmware (an Arduino sketch uploaded to the Arduino board), this class allows you 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.


Nested Class Summary
 class Arduino.SerialProxy
          This class is not intended to be used elsewhere.
 
Field Summary
 
Fields inherited from interface nl.tue.id.creapro.arduino.ArduinoConstants
HIGH, INPUT, LOW, OUTPUT
 
Fields inherited from interface nl.tue.id.creapro.arduino.SerialConstants
ANALOG_MESSAGE, ANALOG_PULL_INTERVAL, ANALOG_STEP, COMMAND_COUNT, CONFIG, DEBUG, DIGITAL_MESSAGE, EVENT_COUNT, READ_COUNT, REPORT_ANALOG_PIN, REPORT_DIGITAL_PORTS, REPORT_VERSION, SERIAL_PULL_INTERVAL, SET_DIGITAL_PIN_MODE, WRITE_COUNT
 
Constructor Summary
Arduino(processing.core.PApplet parent, java.lang.String name)
          Creates a proxy to an Arduino board running the IDuino firmware.
Arduino(processing.core.PApplet parent, java.lang.String name, int rate)
          Creates a proxy to an Arduino board running the IDuino firmware.
 
Method Summary
 void addArduinoListener(ArduinoListener listener)
          Registers an input event listener.
 int analogRead(int pin)
          Reads the last known value read from the analog pin: 0 (0 volts) to 1023 (5 volts).
 void analogWrite(int pin, int value)
          Writes an analog value (PWM wave) to a pin.
 int digitalRead(int pin)
          Returns the last known value read from the digital pin: HIGH or LOW.
 void digitalWrite(int pin, int value)
          Write to a digital pin (the pin must have been put into output mode with pinMode()).
 void disableAnalogInput(int pin)
          Disables a particular analog input pin.
 void disableDigitalInput(int pin)
          Disables a particular digital input pin.
 void enableAnalogInput(int pin)
          Enables a particular analog input pin, otherwise no input from this pin will be reported to the host computer.
 void enableDigitalInput(int pin)
          Enables a particular digital input pin, otherwise no input from this pin will be reported to the host computer.
static java.lang.String[] list()
          Get a list of the available Arduino boards; currently all serial devices (i.e.
 void pinMode(int pin, int mode)
          Set a digital pin to input or output mode.
 void removeArduinoListener(ArduinoListener listener)
          Removes an listener from the list of listeners.
 void setAnalogPullInterval(int interval)
          Sets the analog pull interval (ms).
 void setAnalogStep(int step)
          Sets the setup of the analog input value.
 void setSerialPullInterval(int interval)
          Sets the serial pull interval (ms).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Arduino

public Arduino(processing.core.PApplet parent,
               java.lang.String name)
Creates a proxy to an Arduino board running the IDuino firmware. it uses a default baud rate 57600 to communicate with the Arduino board.

Parameters:
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

public Arduino(processing.core.PApplet parent,
               java.lang.String name,
               int rate)
Creates a proxy to an Arduino board running the IDuino firmware.

Parameters:
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.
Method Detail

addArduinoListener

public void addArduinoListener(ArduinoListener listener)
Registers an input event listener. The listener must implement the ArduinoListener interface. Upon an input event is received by the Arduino proxy, all registered event listeners will be notified.

Parameters:
listener - the listener to be registered.

analogRead

public int analogRead(int pin)
Reads the last known value read from the analog pin: 0 (0 volts) to 1023 (5 volts).

Parameters:
pin - the analog pin whose value should be returned (from 0 to 5)
Returns:
returns the last known value read from the analog pin.

analogWrite

public void analogWrite(int pin,
                        int 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.

Parameters:
pin - the pin to write to
value - the duty cycle: between 0 (always off) and 255 (always on).

digitalRead

public int digitalRead(int pin)
Returns the last known value read from the digital pin: HIGH or LOW.

Parameters:
pin - the digital pin whose value should be returned (from 2 to 13, since pins 0 and 1 are used for serial communication)
Returns:
the last known value read from the digital pin: HIGH or LOW

digitalWrite

public void digitalWrite(int pin,
                         int value)
Write to a digital pin (the pin must have been put into output mode with pinMode()).

Parameters:
pin - the pin to write to (from 2 to 13)
value - the value to write: Arduino.LOW (0 volts) or Arduino.HIGH (5 volts)

disableAnalogInput

public void disableAnalogInput(int pin)
Disables a particular analog input pin. No input from this pin will be reported to the host computer after this message is received by the Arduino board.

Parameters:
pin - the analog pin to be disabled (from 0 to 5)

disableDigitalInput

public void disableDigitalInput(int pin)
Disables a particular digital input pin. No input from this pin will be reported to the host computer after this message is received by the Arduino board.

Parameters:
pin - the digital pin to be disabled (from 2 to 13)

enableAnalogInput

public void enableAnalogInput(int pin)
Enables a particular analog input pin, otherwise no input from this pin will be reported to the host computer.

Parameters:
pin - the analog pin to be enabled (from 0 to 5)

enableDigitalInput

public void enableDigitalInput(int pin)
Enables a particular digital input pin, otherwise no input from this pin will be reported to the host computer. When a digital pin is set to INPUT mode (Arduino.INPUT, using function pinMode(pin, mode)), it is automatically enabled.

Parameters:
pin - the digital pin to be enabled (from 2 to 13)

list

public static java.lang.String[] list()
Get a list of the available Arduino boards; currently all serial devices (i.e. the same as Serial.list()).

Returns:
the string[]

pinMode

public void pinMode(int pin,
                    int mode)
Set a digital pin to input or output mode.

Parameters:
pin - the pin whose mode to set (from 2 to 13)
mode - either Arduino.INPUT or Arduino.OUTPUT

removeArduinoListener

public void removeArduinoListener(ArduinoListener listener)
Removes an listener from the list of listeners. The listerner will then no longer been notified with the input events.

Parameters:
listener - the listener to be removed

setAnalogPullInterval

public void setAnalogPullInterval(int interval)
Sets the analog pull interval (ms). IDuino firmware tries to check the values from the analog input pins every 200 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.

Parameters:
interval - the new analog pull interval in milliseconds (0-1023).

setAnalogStep

public void 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.

Parameters:
step - the new analog step (0-1023). 5 by default.

setSerialPullInterval

public void setSerialPullInterval(int interval)
Sets the serial pull interval (ms). IDuino firmware tries to check the values from the serial port as frequent as possible (by default). This interval can be changed by setting a different pull interval, especially when it is not necessary to check the communication so often.

Parameters:
interval - the new serial pull interval in milliseconds (0-1023).