if-do

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. 1 x ZumBloq LED
  2. 1 x ZUMbloq light sensor
  3. 1 x ZumBloq push-button
  4. A ZUM BT 328 controller board or one compatible with Arduino UNO
  5. A USB cable

The If… do block

Learning  about conditional sentences

Now hold on a minute. A conditional sentence? What´s that?

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

First of all, connect your Zumbloq LED and your Zumbloq button to your board, as shown in this diagram:
zum-3led-4pulsWe want the LED to light up on pressing the button.

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:

programaejemplo1pulsador1_en
Remember that depending on the type of button you have, it could have a value of 1 or 0 when pressed. If you have a one of our robotics kits, the value on pressing the button will be 0. 

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:

 

There is also another option in this block, the else if option. This option enables more conditions to be added after the first one. An example: If we want to eat cake, but there isn´t any cake, (else) if there´s ice cream – that will do instead, right? We want to eat ice cream! Although we prefer cake! This is very important! We  will only eat ice cream if there is NO cake.

Now we will abandon our dear button and do a different exercise using the light sensor and the else if option:

Example 2

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:
sensordeluzledRemember 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.

reloj-de-arena-10375

If everything has gone right, your program should look something like this:

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:

 

 

What do you think? You might have some doubts about using the block. Don´t worry! Practice makes perfect! Soon you will find that when you want to program something, you know exactly how to do it.  And the if… do block will become your new best friend. Mmm… cake…