Page 73 - The Unofficial Guide to Lego Mindstorms Robots
P. 73
62
Edges and Pulses
If you examine the output of a touch sensor, over time, it looks something like this:
The transitions from 0 to 1 and from 1 to 0 are called edges. A transition from 0 to 1 is a rising edge, while a transition from 1
to 0 is a falling edge.
T he SENSOR_EDGE configuration counts all edges, rising or falling. SENSOR_PULSE is a little more selective—it counts
ri sing edges only.
T he following example plays a sound after every fourth press on the touch sensor. It begins by configuring input 1 to count
touch presses with the SENSO R_P ULSE configuration. Then it enters an endless loop, repeatedly testing the value of input 1.
If it is 4 , a sound is played, and the count for inpu t 1 is reset.
task main() {
SetSensor(SENSOR_1, SENSOR_PULSE);
while(true) {
if (SENSOR_1 == 4) {
PlaySound(SOUND_DOWN);
ClearSensor(SENSOR_1);
}
}
}
The SetSensor() command actually configures an input's type and mode at the same time. The input type describes the
e lectrical characteristics of the att ached sensor, while the mode determines how the sensor values are interpreted. If you need
finer control over the inputs than you can get from SetSensor(), use the SetSensorType() and SetSensorMode()
commands: