
408 MAKING THINGS TALK
// 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) {
// if serialEvent occurs at all, contact with the microcontroller
// has been made:
madeContact = true;
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
//and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// if you received all the sensor strings, use them:
if (sensors.length >= 2) {
vals[0] = sensors[0];
vals[1] = sensors[1];
// send out the serial port to ask for data:
myPort.write('\r');
}
}
}
Chapter 9
Color Recognition Using a Webcam
Language: Processing
Reads an image from a camera and looks for a blob of a
particular color. Click on a color in the image to choose the
color to track.
/import processing.video.*;
Capture myCamera; // instance of the Capture class
color targetColor = color(255,0,0); // the initial color to find
int[] matchingPixel = new int[2]; // matching pixel's coordinate
void setup()
{
// set window size:
size(320, 240);
// List all available capture devices. Macintoshes genera