Power without control…
Today we are going to learn how to use the blocks of the Control tab, or more specifically the if… do block
How boring, right? No, not at all! These blocks are the essential element to making your destroyer robot (or vacuum cleaner) conquer the world! (or at least leaving it nice and clean).
Let´s get down to it!
Today you will need:
- 1 x ZumBloq LED
- 1 x ZUMbloq light sensor
- 1 x ZumBloq push-button
- A ZUM BT 328 controller board or one compatible with Arduino UNO
- A USB cable
The If… do block
Learning about conditional sentences
A conditional sentence is a condition, something that happens so that part of the program will be executed.
It´s really very simple. For example: If you´ve been good, Santa will bring you presents. If you don´t tidy your room, you´ll be in big trouble with your mum. If I press this button, the LED will light up.
We´ve already used this block a number of times before, right? It´s an essential block that will enable us to tell our program what to do and when.
So how does it work? Let´s take a look at the next example:
Example using the If… do block
Using the If… do block, make the LED light up when the button is pressed
In other words:
If the button is pressed… the LED lights up So we need to use the if… do block, the program will look like this:
As you can see, the LED lights up on pressing the button and it remains on indefinitely… and because our board is very stupid… it only does what we tell it to do! So we also have to tell it what happens when the button is not pressed!
How do we do this? Now look carefully at the program:
As you will see, the If… do can have more options, such as else which will be executed is the condition is not fulfilled, i.e. when our button is not pressed. To add the else option, hit the star on the block then add it:
Now we will abandon our dear button and do a different exercise using the light sensor and the else if option:
Making an LED flash on and off when the light sensor detects low light, and light up when in darkness
I have something to confess, I´m scared of the dark. I don´t use the bathroom in the night. Even if I really need to, I wait till the next morning because I can´t stand walking through the halfway in darkness. Who knows what monsters are lurking, maybe the maths teacher…
That´s why I decided to put my hands to work and invent a night alarm. So that I´ll never be left in the dark. I was talking to a reputable lighting technician and, after much discussion, we reached the conclusion that this revolutionary machine of mine that would change the childhood of humanity, would have to have the following form:
-If the quantity of light is less than 100 -> Hey we´re all in darkness! ->THE LED LIGHTS UP
-But, if the quantity of light is less than 200, well, it´s still kind of scary… ->THE LED FLASHES ON AND OFF!
-but, if the sun is shining, no worries… I´m brave! ->THE LED GOES OFF. Laugh at the iPhone! Let´s get to work. The first thing we need to do is the connections on the board:
Remember that the light sensor is an analogue component.
So you will need to connect it to the analogue pins on your board.
Now that you have everything connected, why don´t you have a go at programming it yourself? You will get it right soon enough.
Once you have done it, scroll down to check you have got it right.
We will soon be looking at logical operations in more detail, but in case you wanted to set more than condition in the if… do block, for example, that the button is pressed and the light value is less than 30, you can do it like this: