Professional Documents
Culture Documents
Processing and Arduino PDF
Processing and Arduino PDF
Processing and Arduino PDF
http://www.cla.purdue.edu/vpa/etb/
Fabian Winkler
rev. 3
What is Processing?
From http://www.processing.org: Processing is an open source programming language
and environment for people who want to program images, animation, and interactions.
It is used by students, artists, designers, researchers, and hobbyists for learning,
prototyping, and production. It is created to teach fundamentals of computer
programming within a visual context and to serve as a software sketchbook and
professional production tool. Processing is developed by artists and designers as an
alternative to proprietary software tools in the same domain.
Menu
Toolbar
Tabs
Text editor
Message area
Console
Mark a word in the Processing code and ctrl click (Macintosh) on it to display
information about it in the Processing reference.
Example:
Sketch name: Sketch_01", the directory for the sketch will be called "Sketch_01",
the main file will be called "Sketch_01.pde".
Processing has become an extremely powerful scripting environment for the inclusion of
almost any type of media mainly through the concept of libraries. These libraries
consist of subroutines and code that extend the functionality of Processing, often to
include a particular kind of media (such as video, sound or 3D graphics) or functionality
(communications, interface design, fullscreen playback, etc.).
To install a library, unzip the file into the libraries folder in your sketchbook (on the
Macintosh you find the sketchbook in your home directory > Documents > Processing).
If you do not already have a libraries folder in your sketchbook, create this folder
manually and unzip the files in there. In general, most libraries have instructions on the
website where you can download them from, following these directions is the easiest
way to install a library.
For a thorough introduction to the Arduino board see: Banzi, Massimo. Getting Started
with Arduino (Make: Projects). Sebastopol, CA: Make Books, 2008.
Additionally, you can also read pp. 633-657 in Processing A Programming Handbook
for Visual Designers and Artists by Casey Reas and Ben Fry for an excellent introduction
on how to connect the Arduino board with Processing and for code and circuit
examples.
Pushbutton switch
int switchPin = 7;
int LEDPin = 13;
void setup() {
pinMode (switchPin, INPUT);
pinMode (LEDPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(switchPin) == HIGH) {
Serial.print(0, BYTE);
digitalWrite(LEDPin, LOW);
} else {
Serial.print(1, BYTE);
digitalWrite(LEDPin, HIGH);
}
delay(100);
}
Processing Code:
import processing.serial.*;
Serial port;
int val;
void setup() {
size(400, 400);
noStroke();
frameRate(10);
println(Serial.list());
// print a list of all available ports
void draw() {
if (0 < port.available()) {
val = port.read();
}
background(204);
if (val == 0) {
fill(255, 127, 0); // fill orange
} else {
fill(0, 170, 0); // fill green
}
rect(50, 50, 300, 300);
}
void setup()
{
size(400, 300);
frameRate(10);
images[0] = loadImage("money_0000.jpg");
images[1] = loadImage("money_0001.jpg");
images[2] = loadImage("money_0002.jpg");
images[3] = loadImage("money_0003.jpg");
images[4] = loadImage("money_0004.jpg");
images[5] = loadImage("money_0005.jpg");
images[6] = loadImage("money_0006.jpg");
images[7] = loadImage("money_0007.jpg");
images[8] = loadImage("money_0008.jpg");
images[9] = loadImage("money_0009.jpg");
images[10] = loadImage("money_0010.jpg");
images[11] = loadImage("money_0011.jpg");
images[12] = loadImage("money_0012.jpg");
images[13] = loadImage("money_0013.jpg");
images[14] = loadImage("money_0014.jpg");
void draw()
{
if (frame<numFrames-1) {
frame = (frame+1);
} else {
frame = 0;
}
image(images[frame], 0, 0);
}
Processing code:
import processing.serial.*;
Serial port;
int val = 0;
int oldval = 0;
int numFrames = 15; // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
void setup()
{
size(400, 300);
frameRate(10);
images[0] = loadImage("money_0000.jpg");
images[1] = loadImage("money_0001.jpg");
images[2] = loadImage("money_0002.jpg");
images[3] = loadImage("money_0003.jpg");
images[4] = loadImage("money_0004.jpg");
images[5] = loadImage("money_0005.jpg");
images[6] = loadImage("money_0006.jpg");
images[7] = loadImage("money_0007.jpg");
images[8] = loadImage("money_0008.jpg");
images[9] = loadImage("money_0009.jpg");
images[10] = loadImage("money_0010.jpg");
images[11] = loadImage("money_0011.jpg");
images[12] = loadImage("money_0012.jpg");
images[13] = loadImage("money_0013.jpg");
images[14] = loadImage("money_0014.jpg");
println(Serial.list());
// print a list of all available ports
void draw()
{
if (0 < port.available()) {
image(images[frame], 0, 0);
oldval = val;
}
import processing.serial.*;
Serial port;
int val = 0;
int oldval = 0;
int numFrames = 15; // The number of frames in the animation
int rand_frame = 0;
PImage[] images = new PImage[numFrames];
void setup()
{
size(400, 300);
frameRate(10);
images[0] = loadImage("money_0000.jpg");
images[1] = loadImage("money_0001.jpg");
images[2] = loadImage("money_0002.jpg");
images[3] = loadImage("money_0003.jpg");
images[4] = loadImage("money_0004.jpg");
images[5] = loadImage("money_0005.jpg");
images[6] = loadImage("money_0006.jpg");
images[7] = loadImage("money_0007.jpg");
images[8] = loadImage("money_0008.jpg");
images[9] = loadImage("money_0009.jpg");
images[10] = loadImage("money_0010.jpg");
images[11] = loadImage("money_0011.jpg");
images[12] = loadImage("money_0012.jpg");
images[13] = loadImage("money_0013.jpg");
images[14] = loadImage("money_0014.jpg");
println(Serial.list());
// print a list of all available ports
void draw()
{
if (0 < port.available()) {
val = port.read();
}
image(images[rand_frame], 0, 0);
oldval = val;
}
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer mySound;
Serial port;
int val = 0;
int oldval = 0;
int playback_pos = 0;
void setup() {
size(400, 400);
noStroke();
frameRate(10);
println(Serial.list());
// print a list of all available ports
void draw() {
if (0 < port.available()) {
val = port.read();
}
background(204);
void stop()
{
// always close Minim audio classes when you are done with them
mySound.close();
minim.stop();
super.stop();
}
Serial port;
Movie myMovie;
int val = 0;
void setup() {
size(320, 240);
background(0);
myMovie = new Movie(this, "spooky2.mov");
myMovie.loop();
println(Serial.list());
// print a list of all available ports
void draw() {
background(255);
if (0 < port.available()) {
val = port.read();
}
image(myMovie, 0, 0);
if (val == 0) {
myMovie.speed(0);
} else {
myMovie.speed(1);
}
}
Arduino code:
int inA = 5;
int inB = 6;
int inC = 7;
int inD = 8;
int inE = 9;
int inF = 10;
int current_inA = 0;
int previous_inA = 0;
int current_inB = 0;
int previous_inB = 0;
int current_inC = 0;
int previous_inC = 0;
int current_inD = 0;
int previous_inD = 0;
int current_inE = 0;
int previous_inE = 0;
int current_inF = 0;
int previous_inF = 0;
int current_inG = 0;
int previous_inG = 0;
void setup() {
pinMode (inA, INPUT);
pinMode (inB, INPUT);
pinMode (inC, INPUT);
pinMode (inD, INPUT);
pinMode (inE, INPUT);
pinMode (inF, INPUT);
pinMode (inG, INPUT);
void loop() {
switch (current_inA){
case HIGH:
Serial.print("A"); // identifier for sensor A
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("A"); // identifier for sensor A
switch (current_inB){
case HIGH:
Serial.print("B"); // identifier for sensor B
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("B"); // identifier for sensor B
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
switch (current_inC){
case HIGH:
Serial.print("C"); // identifier for sensor C
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("C"); // identifier for sensor C
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
switch (current_inD){
case HIGH:
case LOW:
Serial.print("D"); // identifier for sensor D
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
switch (current_inE){
case HIGH:
Serial.print("E"); // identifier for sensor E
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("E"); // identifier for sensor E
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
switch (current_inF){
case HIGH:
Serial.print("F"); // identifier for sensor F
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("F"); // identifier for sensor F
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
switch (current_inG){
case HIGH:
Serial.print("G"); // identifier for sensor G
Serial.print(0);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
case LOW:
Serial.print("G"); // identifier for sensor G
Serial.print(1);
Serial.print(10, BYTE); // ASCII 10 = newline character, used to
// separate the data strings
break;
}
}
previous_inA = current_inA;
previous_inB = current_inB;
previous_inC = current_inC;
previous_inD = current_inD;
previous_inE = current_inE;
previous_inF = current_inF;
previous_inG = current_inG;
Processing code:
import processing.serial.*;
int NEWLINE = 10; // this is the ASCII code for a newline character
int input_A = 0;
int input_B = 0;
int input_C = 0;
int input_D = 0;
int input_E = 0;
int input_F = 0;
int input_G = 0;
Serial port;
void setup() {
size(660, 480);
noStroke();
println(Serial.list());
// print a list of all available ports
void draw() {
if(serial != NEWLINE) {
buff += char(serial);
} else {
if (buff.length() > 1) {
temp = buff.substring(0, buff.length()-(buff.length()-1));
// this isolates just the first letter to identify the sensor
}
}
}
potentiometer
The following example illustrates how you can control 256 lines in a Processing window
with a potentiometer connected to the Arduino board. If the potentiometer is at its
minimum limit, the Processing sketch will draw just one line on the left hand side of the
screen. The more the potentiometers knob is turned the more lines will be drawn on
the screen until the whole screen is filled with 256 lines (potentiometer at its maximum
limit). This example uses a for loop to iterate through a set of numbers ranging from
0 to the current number that the Arduino board sends from the potentiometer. This
number determines the number of lines drawn on the screen and their distribution. We
also use this number to gradually change the color of the lines drawn to the screen.
The code for the Arduino board is the following:
int val;
int inputPin = 2; // this is the pin to which we
// connect the potentiometer
void setup() {
Serial. begin(9600);
}
void loop() {
val = analogRead(inputPin)/4; // the potentiometers range is from
// 0-1023 but we only need numbers from
Processing code:
import processing.serial.*;
Serial port;
int val;
void setup() {
size(1000, 500);
//noStroke();
frameRate(10);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
// choose the port that the Arduino is
// connected to, on a Macintosh choose
// the tty.usbserial port
}
void draw() {
if (0 < port.available()) {
val = port.read();
}
background(204);
line (i*5, 100, (i*5)+10, 400); // draw the lines based on the
// potentiometers value
}
println(val); // prints out values from Arduino board in
// the console, just to see what Processing
// actually receives from the Arduino board
}
Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("A");
// Example identifier for sensor 1, send as character
Serial.print(val1); // send sensor 1 value as decimal number
Serial.print(10, BYTE); // send ASCII string "10"
// ASCII 10 = newline character,
// used to separate the data strings
Serial.print("B");
// Example identifier for sensor 2, send as character
Serial.print(val2); // send sensor 2 value as decimal number
Serial.print(10, BYTE); // send ASCII string "10"
// ASCII 10 = newline character,
// used to separate the data strings
Processing code:
Processing Code
Christian Nold, 22 Feb 06
import processing.serial.*;
float xCoordinate = 0;
float yCoordinate = 0;
int val = 0;
int NEWLINE = 10;
Serial port;
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
// change [0] to the correct number of your serial port
// e.g. tty.usbserial on the Mac and COM1 or COM 2 on the PC
void draw()
{
fill(0,2); // use black with alpha 2
rectMode(CORNER);
rect(0,0,width,height);
if(serial != NEWLINE) {
buff += char(serial);
} else {
if (buff.length() > 1) {
xCoordinate = width/(1024/temporary);
println(xCoordinate);
}
yCoordinate = height/(1024/temporary);
println(yCoordinate);
}
}
}