| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

ArduinoProcessingLibraryReference

Page history last edited by PBworks 16 years, 2 months ago

Javadoc

The automatically created Javadoc documentation for the arduino processing library can be found at: http://hci.stanford.edu/tutorials/arduino/arduino-processing-library/doc

 

Overview

Initialization

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.

 

Digital I/O

  • To set a pin to input or output, use
    arduino.pinMode(pin, Arduino.INPUT);
    arduino.setPinMode(pin, Arduino.OUTPUT);
    where pin is an int with value between 2 and 13.
  • To set an output pin high or low, use
    arduino.digitalWrite(pin, Arduino.HIGH);
    arduino.digitalWrite(pin, Arduino.LOW);
  • To read a value from a digital input pin use
    arduino.digitalRead(pin);
    Returns Arduino.HIGH or Arduino.LOW.

 

Analog I/O

  • To write a PWM value to a pin between 9 and 11 use
    arduino.analogWrite(int pin, int value);
    where value is an int variable between 0 and 255.
  • To read an analog value from an analog input pin use
    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).

 

Servos

  • arduino.servoAttach(int pin): Initialize servo timer routines on a pin. Pin must be 9 or 10 in the current implementation.
  • arduino.servoDetach(int pin): Stop pulse output to a pin that was previously initialized with servoAttach().
  • arduino.servoSetMaxPulseTime(int pin, int value): Set the pulse duration (in microseconds) that causes a servo to rotate to its upper limit. This value is usually around 2500 for Futaba servos.
  • ardino.servoSetMinPulseTime(int pin, int value): Set the pulse duration (in microseconds) that causes a servo to rotate to its lower limit. This value is usually around 500 for Futaba servos.
  • arduino.servoWrite(int pin, int value):Write a servo angle (in degrees) to an output pin. value has to be a number between 0 and 180.

 

Event listener functions

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);
}

 

Misc

  • static java.lang.String[] list(): Get a list of the available serial ports.

 

Example code

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.

Comments (0)

You don't have permission to comment on this page.