Choosing from various options
The if… do block that we saw in the previous lesson is ideal when a variable can take two values: For example, the push-button: If pressed… the opposite. However, when a variable can take a serie of values, doing it with if… do is not so simple. For example. Let´s imagine a program that counts the number of times the button is pressed and carries out one action or another depending on this value. To do this, we have the switch block, which is usually known as switch… case in the programming world.
As we said earlier, we are going to show an example where different actions are carried out based on the number of times that the button is pressed. These actions are as follows:
- If pressed once: Light up the LED
- If pressed 3 times: Make the LED blink on and off
- If pressed 5 times: Make the buzzer sound
- In any other case (meaning: 0, 2, 4, 6…): Turn off the LED
You will need:
- 1 x ZumBloq LED
- 1 x ZumBloq buzzer
- 1 x ZumBloq push-button
- A ZUM BT 328 controller board or one compatible with Arduino UNO
- A USB cable
So now you know how bitbloq works, having a go at this example will a piece of cake! You´ve got the program below, so let´s go!