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).


To use Firmata follow these steps:
  1. In Arduino, upload Arduino:hardware:libraries:Standard_Firmata:Standard_Firmata.pde

  2. In Processing, open a new sketch.

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


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

Modify this code so that it works with Firmata. (remember values sent must be between (0 and 255):
import processing.serial.*;

Serial port;

void setup(){
//set the width to 256 and the height to whatever you want
  size(_____,_____);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  

}

void draw(){
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, mouseX, mouseY);
  }

  // write the current X-position of the mouse to the serial port as
  port.write(_____);
}

Modify this code so that it works with Firmata.
            //import processing serial package
import processing.serial.*;


//create a variable of type Serial named port. Do not set it.
____________


void setup() { 
//set the width and height
  size(_____,_____);
  
  noStroke(); 
  
  //set the frameRate to 10
  frameRate(___); 

  // List all the available serial ports in the output pane. 
  // You will need to choose the port that the Arduino board is 
  // connected to from this list. The first port in the list is 
  // port #0 and the third port in the list is port #2. 
  println(Serial.list()); 

  // Open the port that the Arduino board is connected to (in this case #0) 
  // Make sure to open the port at the same speed Arduino is using (9600bps) 
  port = new Serial(this, Serial.list()[0], 9600); 
} 

 

void draw() {
//select a background color in hex format 
  background(#_________); 
  // check if mouse is over square
  if(mouseOverRect()) { 
  // change color 
    fill(#________); 
    // turn on the light
    ____________________
  } else { 
    // change color 
    fill(#________); 
     // turn off the light
     __________________
  } 
  //create a rectangle
  rect(___,___,___,____);  // draw square 
}

// function to test if mouse is over square 
boolean mouseOverRect() { 
  return ((mouseX >= ___)&&(mouseX <= ____)&&(mouseY >= ___)&(mouseY <= ____)); 
}