topical media & game development
graphic-processing-algorithm-Ch11-p285b-p285b.pde / pde
import processing.serial.*;
String buff = “”;
int val = 0;
int NEWLINE = 10;
Serial port;
void setup(){
port = new Serial(this, Serial.list()[0], 9600); // Use the first available port
}
void draw(){
while (port.available() > 0)
serialEvent(port.read()); //look for data
background(val); //change background clr based on input data
}
void serialEvent(int serial) {
if(serial != NEWLINE) {
buff += char(serial); //add on bytes until a newline is found
}
else {
buff = buff.substring(0, buff.length()-1);
val = Integer.parseInt(buff)/4; // Parse the String to int
println(val);
buff = “”; // Clear the value of “buff”
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.