Contents
1. Arduino in Processing
Together with the IDuino firmware (an Arduino sketch uploaded to the Arduino board), 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 to be enabled (from 0 to 5).
- disableAnalogInput(int pin)
- isables 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.
- pin
- the analog pin to be disabled (from 0 to 5)
- 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.
- pin - the digital pin to be enabled (from 2 to 13)
- 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.
- pin
- the digital pin to be disabled (from 2 to 13)
- 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.
- interval
- the new analog pull interval in milliseconds (0-1023).
- 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 (0-1023). 5 by default.
- 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.
- interval
- the new serial pull interval in milliseconds (0-1023).
- 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)
- value
- either Arduino.LOW or Arduino.HIGH
- int analogRead(int 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(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.
- pin
- the pin to write to.
- value
- the duty cycle: between 0 (always off) and 255 (always on).
1.3. Callback functions in Processing
Implementation of the push mode of event handling. To use this in Processing:
- A sensor must be enabled first using enableAnalogInput(int pin) (for analog input), or pinMode(int pin, Arduino.INPUT) (for digital input), depending either the senor is connected to a digital pin or an analog pin. This is usually done in setup() in your Processing code.
- In Processing, define the following functions to be called to receive the input events when input is available. If only digital input is expected, analogAvailable() is not necessary. If only analog input is expected, digitalAvaialbe() is not necessary. if both are expected, both digitalAvaialbe() and analogAvailable should be defined to receive the input events.
- digitalAvailable(int pin, int oldValue, int newValue)
- receives digital input events in Processing.
- pin
- from which the input events is received.
- oldValue
- the old value before the change, either Arduino.HIGH or Arduino.LOW.
- newVaule
- the new value after the change, either Arduino.HIGH or Arduino.LOW.
- analogAvailable(int pin, int oldValue, int newValue)
- receives analog input events in Processing.
- pin
- from which the input events is received.
- oldValue
- the old value before the change, from 0 to 1023.
- newVaule
- the new value after the change, from 0 to 1023.
See also: InputEventHandling.
1.4. Example
The following example shows how an AdMoVeo robot can be controlled in Processing, using the Arduino class. The left distance sensor detects the change of the distance from an object. The measured distance is then used to change the color led on AdMoVeo and the backgroud color of the stage.
1 import processing.serial.*;
2 import nl.tue.id.creapro.arduino.*;
3
4 Arduino arduino;
5 int pin_led_b = 9;
6 int pin_distance_left = 3;
7 int bgcolor = 255;
8
9 void setup()
10 {
11 arduino = new Arduino(this, "COM11");
12 arduino.enableAnalogInput(pin_distance_left);
13 arduino.pinMode(pin_led_b, Arduino.OUTPUT);
14 }
15
16 void draw()
17 {
18 background(0,0,bgcolor);
19 }
20
21 void analogAvailable(int pin, int oldValue, int newValue){
22 if(pin == pin_distance_left){
23 bgcolor = newValue/4;
24 arduino.analogWrite(pin_led_b, bgcolor);
25 }
26 }