
APPENDIX C 381
else {
// if the sensor value is less than the threshold,
// and the previous value was greater, then the cat
// just left the mat
if (prevSensorValue >= threshold) {
catOnMat = false;
}
}
// save the sensor value as the previous value
// so you can take new readings:
prevSensorValue = sensorValue;
if (requestInProgress == true) {
checkNetClient();
}
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);
// if you got any bytes other than the linefeed:
if (myString != null) {
//trim off the carriage return and convert the string to an integer:
sensorValue = int(trim(myString)) /2 -100;
// print it:
// println(sensorValue);
drawGraph();
}
}
void drawGraph() {
int lineHeight = sensorValue /2;
// draw the line:
if (catOnMat) {
// draw green:
stroke(0,255,0);
}
else {
// draw red:
stroke(255,0,0);
}
line(graphPosition, height, graphPosition, height - lineHeight);
// at the edge of the screen, go back to the beginning:
if (graphPosition >= width) {
graphPosition = 0;
background(0);
}
else {
graphPosition++;
}
}
void sendMail() {
// calculate the current tim