Firmata
Firmata is a generic protocol for communicating with microcontrollers from software on a host computer. It is intended to work with any host computer software package. Right now there is a matching object in a number of languages. Basically, this firmware establishes a protocol for talking to the Arduino from the host software. The aim is to allow people to completely control the Arduino from software on the host computer.There's a Processing library that works with Firmata.
These functions are in the Processing Arduino Library and communicate (from Processing) with a Arduino:
Arduino.list(): returns a list of the available serial devices. If your Arduino board is connected to the computer when you call this function, its device will be in the list.
Arduino(parent, name, rate): create an Arduino object. Parent should be "this" (without the quotes); name is the name of the serial device (i.e. one of the names returned by Arduino.list()); rate is the speed of the connection (57600 for the current version of the firmware).
pinMode(pin, mode): set a digital pin to input or output mode (Arduino.INPUT or Arduino.OUTPUT).
digitalRead(pin): returns the value of a digital pin, either Arduino.LOW or Arduino.HIGH (the pin must be set as an input).
digitalWrite(pin, value): writes Arduino.LOW or Arduino.HIGH to a digital pin.
analogRead(pin): returns the value of an analog input (from 0 to 1023).
analogWrite(pin, value): writes an analog value (PWM wave) to a digital pin that supports it (pins 3, 5, 6, 9, 10, and 11); value should be from 0 (always off) to 255 (always on).
Arduino(parent, name, rate): create an Arduino object. Parent should be "this" (without the quotes); name is the name of the serial device (i.e. one of the names returned by Arduino.list()); rate is the speed of the connection (57600 for the current version of the firmware).
pinMode(pin, mode): set a digital pin to input or output mode (Arduino.INPUT or Arduino.OUTPUT).
digitalRead(pin): returns the value of a digital pin, either Arduino.LOW or Arduino.HIGH (the pin must be set as an input).
digitalWrite(pin, value): writes Arduino.LOW or Arduino.HIGH to a digital pin.
analogRead(pin): returns the value of an analog input (from 0 to 1023).
analogWrite(pin, value): writes an analog value (PWM wave) to a digital pin that supports it (pins 3, 5, 6, 9, 10, and 11); value should be from 0 (always off) to 255 (always on).
To use Firmata follow these steps:
- In Arduino, upload Arduino:hardware:libraries:Standard_Firmata:Standard_Firmata.pde
- In Processing, open a new sketch.
- Edit the example code to select the correct serial port:
import processing.serial.*; import cc.arduino.*; Arduino arduino; int ledPin = 13; void setup(){ //println(Arduino.list()); arduino = new Arduino(this, Arduino.list()[0], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); } void draw(){ arduino.digitalWrite(ledPin, Arduino.HIGH); delay(1000); arduino.digitalWrite(ledPin, Arduino.LOW); delay(1000); } - Here's another example. This one uses PWM:
import processing.serial.*; import cc.arduino.*; Arduino arduino; int ____=___; int ____=___; void setup() { size(512, 200); arduino = new Arduino(this, Arduino.list()[0], 57600); arduino.pinMode(_____, Arduino.OUTPUT); arduino.pinMode(_____, Arduino.OUTPUT); } void draw() { background(constrain(mouseX / 2, 0, 255)); arduino.analogWrite(_____, constrain(mouseX / 2, 0, 255)); arduino.analogWrite(_____, constrain(255 - mouseX / 2, 0, 255)); }