/* by nick lally
with help from Jeff Gray, ITP: http://itp.nyu.edu/physcomp/sensors/Code/ThreeSensorsCallResponseHumanReadable
*/
import processing.serial.*;
int val;
Serial port; // The serial port
String serialInString = ""; // Where we'll put what we receive
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(1000,800); // Stage size
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600);
}
void draw() {
println(val);
fill(val);
rect(0, 0, width, height);
}
void serialEvent(Serial port) {
// read a byte from the serial port:
int inByte = port.read();
// if this is the first byte received,
// take note of that fact. Otherwise, add it to the array:
if (firstContact == false) {
if (inByte == 'A') {
port.clear(); // clear the serial port buffer
firstContact = true;
port.write('A');
}
// if firstContact has been made, check to see if a full string has come in
} else {
if(inByte == '\n'){
// split the serialInString into pieces of data
String[] pieces = serialInString.split(",");
// grab strings from array, and convert into ints
val = Integer.parseInt(pieces[1]);
//ypos = Integer.parseInt(pieces[3]);
//fgcolor = Integer.parseInt(pieces[5]);
// print the values (for debugging purposes only):
//println(val + "\t");
// Send a capital A to request new sensor readings:
port.write('A');
// Reset serialInString
serialInString = "";
} else {
serialInString += (char)inByte;
}
}
}