/* by nick lally
with help from Jeff Gray, ITP: http://itp.nyu.edu/physcomp/sensors/Code/ThreeSensorsCallResponseHumanReadable
*/
import processing.serial.*;
int val;
int valPing;
int valTemp;
int valTempFrac;
float soundLevel;
Serial port; // The serial port
String serialInString = ""; // Where we'll put what we receive
boolean firstContact = false; // Whether we've heard from the microcontroller
//minim for sound input
import ddf.minim.*;
Minim minim;
AudioInput in;
void setup() {
size(100,200); // Stage size
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600);
minim = new Minim(this);
minim.debugOn();
// get a line in from Minim, default bit depth is 16
in = minim.getLineIn(Minim.MONO, 512);
}
void draw() {
soundLevel = abs(in.left.get(1)*50);
println(soundLevel);
//println(val);
//println(valPing);
//print(valTemp);
//print(".");
//println(valTempFrac);
fill(soundLevel);
rect(0, 0, width, height/2);
fill(valPing);
rect(0, 0+height/2, width, height/2);
}
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]);
valPing = Integer.parseInt(pieces[3]);
valTemp = Integer.parseInt(pieces[5]);
valTempFrac = Integer.parseInt(pieces[7]);
// 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;
}
}
}
void stop()
{
// always close Minim audio classes when you are done with them
in.close();
minim.stop();
super.stop();
}