Processing Workshop
Useful link for learning how to interface with electronic components:
http://www.wiring.org.co/reference/electronics
http://www.wiring.org.co/learning/tutorials/index.htm
Example 1: (Processing) Play QuickTime movie
import processing.video.*;
Movie myMovie;
void setup(){
size(320,240);
myMovie = new Movie(this, "m1.mov");
myMovie.loop();
}
void draw(){
if(myMovie.available()){
myMovie.read();
}
image(myMovie, 0,0);
}
Example 2: (Processing) QuickTime movie playback
some properties to play with
by adding input:
Example 3: (Processing) More on QT playback - playing multiple movies
import processing.video.*;
Movie m1;
Movie m2;
Movie m3;
int num = 1;
int xpos =0;
int ypos = 0;
int w = 320;
int h = 240;
void setup(){
background(0);
size(320,240);
m1 = new Movie(this, "m1.mov");
m2 = new Movie(this, "m2.mov");
m3 = new Movie(this, "m3.mov");
m1.loop();
m2.loop();
m3.loop();
}
void draw(){
background(255);
if(num == 1){
if(m1.available()){
m1.read();
}
image(m1, xpos, ypos, w, h);
} else if(num==2){
if(m2.available()){
m2.read();
}
image(m2, xpos, ypos, w, h);
} else {
if(m3.available()){
m3.read();
}
image(m3, xpos, ypos, w, h);
}
}
void mousePressed(){
num +=1;
if(num >3){
num = 1;
}
}
Example 4: Control QT playback using analog input
Part 1 - (Arduino) Analog input code revisited
int inpin = 5;
int val = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
val = analogRead(inpin);
Serial.println(val, DEC);
delay(500);
}
Part 2 - (Processing) Reading analog data from Arduino
import processing.serial.*;
Serial myPort;
int val = 0;
int linefeed = 10;
void setup() {
size(320, 240);
String portname = Serial.list()[0];
myPort = new Serial(this, portname, 9600);
noStroke();
smooth();
}
void draw() {
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null){
myString = trim(myString);
val = int(myString);
println(val);
}
}
Exercise: Use Analog data to draw a circle
Display data on the stage
import processing.serial.*;
Serial myPort;
int val = 0;
int linefeed = 10;
PFont font;
void setup() {
size(320, 240);
String portname = Serial.list()[0];
myPort = new Serial(this, portname, 9600);
font = loadFont("Arial-BoldMT-32.vlw");
textFont(font);
textAlign(CENTER);
noStroke();
smooth();
}
void draw() {
background(0);
fill(0,100,255);
text(val, width/2,height/2+10);
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null){
myString = trim(myString);
val = int(myString);
println(val);
}
}
Exercise : Now, can u combine the Analog control with QT playback to create a simple VJ Tool?
………….
Example 6 : Ok, now, let’s try to use multiple analog input…here’s the Arduino code that reads 3 potentiometer at the same time
int pot1 = 3;
int pot2 = 4;
int pot3 = 5;
int val1 = 0;
int val2 = 0;
int val3 = 0;
void setup(){
pinMode(pot1, INPUT);
pinMode(pot2, INPUT);
pinMode(pot3, INPUT);
Serial.begin(9600);
}
void loop(){
val1 = analogRead(pot1)/4;
val2 = analogRead(pot2)/4;
val3 = analogRead(pot3)/4;
Serial.print(val1, DEC);
Serial.print(",");
Serial.print(val2, DEC);
Serial.print(",");
Serial.println(val3, DEC);
}
Processing code
import processing.serial.*;
Serial myPort;
int linefeed = 10;
int val1 = 0;
int val2 = 0;
int val3 = 0;
void setup(){
size(320, 240);
String portname = Serial.list()[0];
myPort = new Serial(this, portname, 9600);
}
void draw(){
background(val1, val2, val3);
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null){
myString = trim(myString);
int vals[] = int(split(myString, ','));
for(int i =0; i<vals.length; i++){
print("Val "+ i + " : " + vals[i] + "\t");
}
println();
val1 = vals[0];
val2 = vals[1];
val3 = vals[2];
}
}
Example 7 : Can you now compose your own DJ box with the following?
And write the processing code to control the playback of the QT movie.
Have fun!
———————————————————-
Example 8 : Drawing in Processing
Basic : Points and Lines
line(p3, p3, p2, p3);
point(p1,p2);
Basic : Shapes
size(200, 200);
smooth();
background(0);
noStroke();
fill(226);
triangle(10, 10, 10, 200, 45, 200);
rect(45, 45, 35, 35);
quad(105, 10, 120, 10, 120, 200, 80, 200);
ellipse(140, 80, 40, 40);
triangle(160, 10, 195, 200, 160, 200);
Creating Motion
float xpos, ypos;
int xdirection = 1;
void setup(){
size(300,300);
noStroke();
smooth();
background(0);
xpos = 0;
ypos = height/2;
}
void draw(){
background(0);
xpos += xdirection;
ellipse(xpos, ypos, 30,30);
}
How to make the ball bounce from the wall?…
Example 9 : Play Pong!
int ballSize = 20;
int paddleWidth = 10;
int paddleHeight =80;
int xDirection = 5;
int yDirection = 5;
int xpos;
int ypos;
int py;
int px = 100;
void setup(){
size(600,400);
background(0);
noStroke();
smooth();
frameRate(25);
xpos = width/2;
ypos = height/2;
}
void draw(){
background(0);
ellipse(xpos, ypos, ballSize, ballSize);
rect(px, py, paddleWidth, paddleHeight);
animateBall();
movePaddle();
}
void animateBall(){
if(xDirection < 0){
if(xpos < (px+paddleWidth+(ballSize/2))){
if(ypos > py && ypos < (py+paddleHeight)){
xDirection *=-1;
}
}
}
if(xpos > (width-ballSize/2) || xpos< ballSize/2){
xDirection *=-1;
}
if(ypos > height-ballSize/2 || ypos < ballSize/2){
yDirection *=-1;
}
xpos = xpos + xDirection;
ypos = ypos + yDirection;
}
void movePaddle(){
py = mouseY;
}
———-
Multiple Input
int potPin = 4;
int photoPin = 5;
int btnPin = 7;
int valPot = 0;
int valPhoto = 0;
int valBtn = 0;
void setup(){
pinMode(potPin, INPUT);
pinMode(photoPin, INPUT);
pinMode(btnPin, INPUT);
Serial.begin(9600);
}
void loop(){
valPot = analogRead(potPin);
valPhoto = analogRead(photoPin);
valBtn = digitalRead(btnPin);
Serial.print(valPot, DEC);
Serial.print(",");
Serial.print(valPhoto, DEC);
Serial.print(",");
Serial.println(valBtn, DEC);
}
Processing
import processing.serial.*;
import processing.video.*;
Movie myMovie;
Movie myMovie2;
Serial myPort;
int linefeed = 10;
int xpos = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
void setup(){
size(320, 240);
String portname = Serial.list()[0];
myPort = new Serial(this, portname, 9600);
myMovie = new Movie(this, "m3.mov");
myMovie.loop();
myMovie2 = new Movie(this, "m2.mov");
myMovie2.loop();
}
void draw(){
background(val1, val2, val3);
if(val3 == 0){
if(myMovie.available()){
myMovie.read();
}
image(myMovie, xpos, 0);
myMovie.jump(val1);
} else {
if(myMovie2.available()){
myMovie2.read();
}
image(myMovie2, xpos, 0, 320,240);
myMovie2.jump(val1);
}
xpos = int(val2*0.5);
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null){
myString = trim(myString);
int vals[] = int(split(myString, ','));
for(int i =0; i<vals.length; i++){
print("Val "+ i + " : " + vals[i] + "\t");
}
println();
val1 = vals[0];
val2 = vals[1];
val3 = vals[2];
}
}
Pong with Potentiometer
import processing.serial.*;
Serial myPort;
int linefeed = 10;
int val = 0;
int ballSize = 20;
int paddleWidth = 10;
int paddleHeight =80;
int xDirection = 5;
int yDirection = 5;
int xpos;
int ypos;
int py;
int px = 100;
void setup(){
size(600,400);
background(0);
noStroke();
smooth();
frameRate(25);
xpos = width/2;
ypos = height/2;
String portname = Serial.list()[0];
myPort = new Serial(this, portname, 9600);
}
void draw(){
background(0);
ellipse(xpos, ypos, ballSize, ballSize);
rect(px, py, paddleWidth, paddleHeight);
animateBall();
movePaddle();
}
void animateBall(){
if(xDirection < 0){
if(xpos < (px+paddleWidth+(ballSize/2))){
if(ypos > py && ypos < (py+paddleHeight)){
xDirection *=-1;
}
}
}
if(xpos > (width-ballSize/2) || xpos< ballSize/2){
xDirection *=-1;
}
if(ypos > height-ballSize/2 || ypos < ballSize/2){
yDirection *=-1;
}
xpos = xpos + xDirection;
ypos = ypos + yDirection;
}
void movePaddle(){
py = val;
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null){
myString = trim(myString);
val = int(myString);
println(val);
}
}
No comments yet. Be the first.
Leave a reply