Hardware Reference
In-Depth Information
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 to the mat.
If he jumps on and off and on again in a
minute or less, you don't want another
email.
Continued from previous page.
myPort.bufferUntil('\n');
// set initial background and smooth drawing:
background(#543174);
smooth();
}
void draw () {
// nothing happens here
}
What does that look like in sensor
terms? To find out, you need to do
one of two things: get the cat to jump
on and off the mat on cue (difficult to
do without substantial bribery, using
treats or a favorite toy), or weigh the
cat and use a stand-in of the same
weight. The advantage to using the cat
is that you can see what happens when
he's shifting his weight, preparing the
bed by kneading it with his claws, and
so forth. The advantage of the stand-in
weight is that you don't have to herd
cats to finish the project.
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
sensorValue = float(inString);
sensorValue = map(sensorValue, 0, 1023, 0, height);
println(sensorValue);
}
}
Refine It
If your system is
working correctly,
you should notice a difference of several
points in the sensor readings when the
cat gets on the mat. It helps to graph
the results so you can see clearly what
the difference looks like. To do that, add
a few extra variables to the variable list
at the beginning of your Processing
program.
float prevSensorValue = 0; // previous value from the sensor
float lastXPos = 0; // previous horizontal position
8
Next, add a new method called
drawGraph() .
void drawGraph(float prevValue, float currentValue) {
// subtract the values from the window height
// so that higher numbers get drawn higher
// on the screen:
float yPos = height - currentValue;
float lastYPos = height - prevValue;
// draw the line in a pretty color:
stroke(#C7AFDE);
line(lastXPos, lastYPos, xPos, yPos);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
ยป
 
Search WWH ::




Custom Search