In this post, we are going to learn the basics of programming: conditional sentences. We will be using a push-button to do this.

What is a conditional sentence? The If…do block

A conditional sentence involving asking a question in order to take action according to the answer. For example, if it rains, I will put up the umbrella. Or else, if it doesn´t rain, I will close it. If it gets cold, I will put my coat on. If it gets hot, I will remove it. If it´s night-time, I will turn on the light. Or else, if it´s daytime, I will switch it off. condicionales

What is a push-button?

pulsador a push-button is a digital sensor which is enabled when pressed (1 or true) and disabled when released (0 or false). We are surrounded by buttons in everyday life, from controls in the lift to the stop button on the bus and your computer keyboard. Together with the conditional sentences, they will be highly useful for controlling our machines and robots.
OJO IMPORTANT: Two types of button exist on the market; ones that are normally open (Pressed->1, Not pressed->0) and ones that are normally closed (Pressed->0, Not pressed->1). If you have a button from My first robotics kit or any other on the market, it will probably be one that is normally closed, so the programs described here work the opposite way, so change 1 for 0 and vice versa when you create your programs.

If it gets dark… turn on the light. Controlling an LED with the button

We are going to learn how to control an LED using the button. Connect the LED to digital pin 13 and the button to digital pin 7.


We want the LED to turn on when we press the button and off when we release it:


In the Control tab, you will find the If… do block. If we press our button, meaning that if the value that the Read Button_1 button returns is equal to 1, we will switch on the LED. Remember that your controller board never does anything without you telling it to do it. You need to program it first to make it do what you want. If you only program the LED to light up when the button returns a value of 1, without telling it to switch off, the LED will remain permanently on. To do this, the If… do block provides two more options: else if and else if, do

Else if…

Else if allows more conditions to be added after the first one. For example: If we want to eat cake, but there isn´t any cake, then 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´s NO cake. In the case of our LED, we want to check that the button is pressed and has the value of 1. If it has a value of 0 rather than 1, the LED will switch off.


Else if, do

The Else if, do block is executed when neither of the previous conditions are met, in other words, when the button is not pressed.

programa_pulsador_led ilustracion_if_pulsado_2r

And there´s more

    Here are some more ideas so you can keep on practising:

  • Connect the LEDs and make one turn on and the other turn off and vice versa, depending on the state of the button.
  • Create a traffic light on red. You can use a different coloured LED if you don´t have a red one. When you press the button, wait five seconds, turn off the red and then switch on the green. After 20 seconds, turn off the green and light up the red again.

Now that you know how conditional sentences and the button work, you can create a whole lot of projects! In the coming lessons, we will find out more and more about sensors and actuators. Stay alert!