Arduino + Processing

The easiest way to make Arduino and processing talk is via Firmata. Firmata is a program that runs on Arduino and allows you to control it from Processing. Basically, you’ll upload Firmata on your board once then you can close the Arduino app (the IDE) and then simply work from Processing.

The full instructions to make arduino talk to Processing through Firmata are here.
The setup is not hard:

1- Unzip the Processing library and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook.
(You can find the location of your Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)

2- Run Arduino, open File > Examples > Firmata > StandardFirmata sketch,
and upload it to the Arduino board. You can now close the Arduino application.

3- Run the processing sketch below, it’s the infamous blinking LED. As you can see the commands are similar to pure Arduino except now you can mix Arduino code and Processing code in the same sketch:

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

Obviously the Arduino needs to be connected to the computer with Processing all the time.

Processing and sensor
This is a more interesting example. You can use this circuit or something that uses an analog and a digital input:

//import the libraries for serial communication
//and for arduino/firmata specifically
import processing.serial.*;
import cc.arduino.*;
 
//this object class Arduino
//represents guess what? Your board
Arduino arduino;
 
//like in an arduino sketch it's good to
//use variables for pin numbers 
int buttonPin = 13;
int sensorPin = 0;
 
void setup() {
  size(500, 500);
  smooth();
  noStroke();
  ellipseMode(CENTER);
 
  //the arduino object needs to be created at the beginning
  arduino = new Arduino(this, Arduino.list()[0], 57600);
 
  //once it's created I set the pin modes  
  arduino.pinMode(buttonPin, Arduino.INPUT);
  arduino.pinMode(sensorPin, Arduino.INPUT); 
}
 
//we are in processing so the main function is draw (not loop)
void draw()
{
  //read a digital value from the button pin
  int digitalValue = arduino.digitalRead(buttonPin);
 
  //change the background accordingly
  if (digitalValue == Arduino.HIGH)
    background(255);
  else
    background(0);
 
  //read an analog value from the sensor pin
  int analogValue =  arduino.analogRead(sensorPin);
  println(analogValue); //print it for testing purposes
 
  //draw a red circle ellipse the size of the analog value
  fill(255,0,0);
  ellipse(width/2, height/2, analogValue, analogValue);
}

Some exercises:
– Threshold switch, if the sensor reaches a certain value the background changes.

– Combine this example with your order and chaos processing sketch, replacing the pressure sensor with a slider or another analog sensor.

– Team up with a friend and use two knobs potentiometers to make a digital etch-a-sketch. One pot controls x, controls y, the red buttons clear the drawing.

Posted in

Post a comment