Cricket Tutorial — Counting Switch Presses

Suppose you want to trigger an action after the switch is pressed 3 times. Then, you need to detect when the switch is pressed down, and when it is released.


                     Run Button ^

Type the procedure on the right into the Procedure window and type test into the Run This line, or .

Then click Download and the Cricket's Run Button.

The trick is the statement waituntil [not switcha]. Notice the not command—this makes the Cricket wait until the switch is released.

So, the first waituntil [switcha] will wait until you press the switch; then the following waituntil [not switcha] waits for you to release it. Then the Cricket beeps.

This repeats a total of 3 times, and then the Cricket turns on the motors for 3 seconds.

to test
waituntil [switcha]
waituntil [not switcha]
beep
waituntil [switcha]
waituntil [not switcha]
beep
waituntil [switcha]
waituntil [not switcha]
beep
onfor 30
end

Suppose you wanted 5 button presses before the motors turn on—can you re-write the procedure to be shorter?

Suppose you didn't use the not command, and you had a program like this (on the right). Would this work? Why or why not? (You can type it and try it.)

to test
waituntil [switcha]
beep
waituntil [switcha]
beep
waituntil [switcha]
beep
onfor 30
end

BACK: Light Sensor with Waituntil

UP to index

BACK: Counting Presses with a Variable