
102 MAKING THINGS TALK
Continued from previous page.
// I know that the first port in the serial list on my Mac is always my
// Arduino, so I open Serial.list()[0]. Open whatever port you're using
// (the output of Serial.list() can help; the are listed in order
// starting with the one that corresponds to [0]).
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(linefeed);
}
void draw() {
// twiddle your thumbs
}
// 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));
// print it:
println(sensorValue);
}
}
You don’t want Processing sending a
message constantly, because you’d get
several thousand emails every time the
cat sits on the mat. Instead, you want
to recognize when the cat’s there, send
an email, and don’t send again until
he’s left and returned again. If he jumps
on and off and on again in a minute or
less, you don’t want to send again.
What does that look like in sensor
terms? ...