Cricket Tutorial — Switch Sensors with If

The waituntil command is great if you can just set the motors going and wait until the sensor is triggered, but what if you want to be performing some actions while you check to see if the sensors are triggered? Or, if you want to do one thing if switcha is pressed, but another if switchb is pressed? Then you need to use the if statement, usually in a continuous loop.


                     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.

What does the program do?

to test
loop [
  if switcha [a, onfor 5]
  if switchb [b, onfor 5]
]
end

First, it makes in infinite loop using the loop command. Everything inside the loop's brackets [ ... ] will run forever — or until the Cricket is turned off or reset.

Inside the loop, there are two if statements. The first checks switcha; if it is pressed, then motor A is run for 1/2 second. The second if statement checks switchb; if it is pressed, then motor B runs for 1/2 second.

What do you think will happen if the toggle switch is set to “on” and the pushbutton switch is held down?

BACK: Cereal Dispenser Stage 2

UP to index

NEXT: Light Sensor and Send