Hardware Reference
In-Depth Information
You don't just want to respond to events, though. You
might have a fickle cat who jumps on and off the mat a
lot. Once you've sent a message, you don't want to send
another one right away, even if the cat gets off the mat and
back on. Decide on an appropriate interval, wait that long,
and don't respond to any input during that time. Once the
interval's over, start looking for input again.
Add the following new variable
to the beginning of your program.
Determine the threshold value by
watching the sensor value without the
cat on the mat, and picking a number
that's higher.
8
int threshold = 250; // above this number, the cat is on the mat.
Then put this code in your serialEv-
ent() method, right after drawGraph()
(new lines are shown in blue). Remove
or comment out the line in the serialEv-
ent() method that prints the sensor
value.
8
drawGraph(prevSensorValue, sensorValue);
if (sensorValue > threshold ) {
// if the last reading was less than the threshold,
// then the cat just got on the mat.
if (prevSensorValue <= threshold) {
println("cat on mat");
sendMail();
}
}
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) {
println("cat not on mat");
}
}
8
Finally, add a method that sends
mail. For now, it will just print a place-
holder to the message window. After
the next section, you'll write code to
make it send mail for real. Add this
method to the end of your program.
void sendMail() {
println("This is where you'd send a mail.");
}
When you run the program, you should see messages
in the console area saying when the cat jumps on or
off the mat, and when a mail would be sent. Your cat
may be fickle or may take his time settling on the mat,
which can result in several mail messages as he gets
comfortable. To avoid this, you want to change the
sketch so that once it sends a message, it doesn't send
any others for an acceptable period. You can do this by
modifying the sendMail() method to keep track of when
the last message was sent. Here's how to do it.
 
Search WWH ::




Custom Search