Hardware Reference
In-Depth Information
8
NOTE: Once you've got the serial con-
nection between the microcontroller and the
computer working, you might want to add in
the Bluetooth radio from the Monski Pong
project in Chapter 2. It will make your life
easier if your computer doesn't have to be
tethered to the cat mat in order to program.
xPos = 0;
lastXPos = 0;
background(#543174);
}
else {
// increment the horizontal position:
xPos++;
// save the current graph position
// for next time:
lastXPos = xPos;
}
}
Finally, add the following to the
serialEvent() method, right after
you print the sensor value (new lines
are shown in blue).
8
println(sensorValue);
sensorValue = map(sensorValue, 0, 1023, 0, height);
drawGraph(prevSensorValue, sensorValue);
// save the current value for the next time:
prevSensorValue = sensorValue;
}
}
When you run the program, you'll see a graph
of the sensor values, as shown in Figure 3-11.
When the cat jumps on the mat, you should
see a sudden increase, and when he jumps off, you'll see
the graph decrease. You'll also see any small changes,
which you might need to filter out. If the changes are
small relative to the difference between the two states
you're looking for, you can ignore them. Using the sensor
values, you have enough knowledge to start defining the
cat's presence on the mat as an event . The event you care
about is when the sensor reading increases significantly,
because that's when the cat sat on the mat. To do this,
pick a threshold number in between the two states. When
the sensor reading changes from being less than threshold
to greater than or equal to it, that's your first event. When
the sensor value goes below the threshold, the cat has left
the mat. That's your second event.
Figure 3-11
Output of the sensor-graphing program.
 
Search WWH ::




Custom Search