Switch on a LED and play with the buttons

In this part we'll program Thymio so that it changes colour when the buttons are pressed.

The LEDs

A LED (light emitting diode) is a device with which light can be made on the robot. A LED has a colour which is fixed when it is manufactured. For example, there are red LEDs at the front of the robot, green ones on top, etc.

We have seen that the robot can change colour. How is it done? In fact, there are three different LEDs very close together, a red one, a green one and a blue one. These three colours are called primary colours because by mixing them all other colours can be created. For example, if blue and red are lit together, it gives magenta. These grouped LEDs are called RGB LEDs (for red, green, blue).


Lighting a LED

To switch on a light on the robot, we are going to code, that is to say we'll write instructions for the robot.

If you click on Native Functions -> leds you can see a list of functions (a function is one of the possible instructions for the robot). With these functions we can switch on the different LEDs. For example leds.top lights up the top RGB LED.


In the main window, type this:

call leds.top(0,0,32)

call is used to call a function; leds.top switches on the RGB LED on top of the robot.

If you leave the cursor over the name of the function, you will see instructions appear saying how to use it. When you call a function, you can give information to the function between the parentheses (). These are called arguments.


In our case there are 3 arguments: the values for the three parts of the RGB LED: red, green and blue. The value 0 means off (no light), while 32 means fully on (brightest). Values in between give a dimmer light.

Now click on Load, then Run. The robot carries out the instruction. You can see that the top LED lights up blue.

If you change the code to:

call leds.top(32,0,0)

the LED will be red. Similarly:

call leds.top(0,32,32)

gives a turquoise LED (green + blue). You can try changing the values to see what colours it gives.


At the moment, as soon as the 'Run' button is clicked, the robot carries out the code (switch on a LED). We may want to link that with a button, for example, so that the LED is switched on when the button is pressed.

To detect a button being pressed, you can check the variable in the Variables window:


Here you can see that the forward button is pressed.

In this case, we will use the condition if … then … else … end. The idea is to be able to tell the robot: if the button is pressed, then turn on the LED. In Aseba, it will be written in the following manner:

if button.forward==1 then    # if the forward button is pressed
    call leds.top(0,0,32)    # then we light the LED in blue
    call leds.top(0,0,0)     # otherwise we turn it off

This code specifies that when the forward button (button.forward) is pressed, the top LED turns blue, otherwise it is switched off. Look at what follows the # sign, which is in English, not Aseba. These are comments, notes for us, not for the robot, so we put them following the #.

Does this code do what we want it to?

No. When you try this code, if you press and release the button the robot does not change colour. We see that it works only once, at the start of the program. If we want the light to turn on every time we press the button, we have to check the state of the button regularly. To achieve this we will use an event.


An event is a moment, determined by a certain set of conditions, at which some code can be executed. An event can occur several times! For example, if you look at the left bottom box of Aseba Studio, there is a list of local events.


The buttons event corresponds to each time we check if a button is pressed. Similarly, buttons.forward corresponds to "each time we check if the forward button is pressed". So we can just add one line in front of our previous code :

# each time the buttons are checked
onevent button.forward
if button.forward==1 then    # if the forward button is pressed
    call leds.top(0,0,32)    # then we light the LED in blue
    call leds.top(0,0,0)     # otherwise we turn it off

If we test this code (Load, Run), we'll see that the robot's LED lights up blue whenever we press the front button, and switches off when we stop. There are many other available events: motor for the motors, acc for the accelerometer etc.

previous page | next page

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License