Using two LEDs with bitbloq
In the last entry Programming an LED using bitbloq (I), we learned how to switch on an LED using bitbloq, as well as how to switch it off and make it flash. Today we will use this knowledge to manage two LEDs and little by little, to make our programs a bit more interesting!
|Today you will need:
Let´s get started!
Switching on two LEDs
The first thing we will do is switch on both LEDs simultaneously. Remember how we switched on an LED in the first example? It´s just as easy with two LEDs!
We will follow the steps below to resolve the problem.
We will connect one of the LEDs to digital PIN 7 and we will connect the second to digital PIN 8. Remember to connect each pin to the matching colour! Do it as shown in this diagram:
To create the program, bear in mind that we have connected two LEDs this time. So we will need to add two blocks in bitbloq and assign each one with the pin connected to it and a state, which in this case will be on.
As we can see, after programming the board, both LEDs switch on and remain on. Why don´t you have a go at making both LEDs flash on and off at the same time?
Making two LEDs flash on and off
Program an application in bitbloq to make both LEDs flash, with a 1 second gap between switch-on and switch-off.
When you have done it, have a look at the solution below.
It´s likely that you will have done something like this:
There´s one important thing to remember: the controller board can only do one thing at a time, which is why the actions programmed can only be executed one after the other. When there´s no waiting time, these actions are executed so quickly that they seem to take place simultaneously. This is why it´s always recommendable to avoid blocks in parallel when programming. So the correct solution is this:If we have programmed the board correctly, both LEDs should switch on and switch off in unison. So what happens when we want two LEDs to flash on and off alternatively (i.e. one switches on when the other switches off and vice versa)? It´s really so simple, we will give you another exercise for you to create the program:
Guided exercise 2
Making two LEDs flash on and off alternatively
Making two LEDs flash so that one is always off and the other is on (with one second of latency) means: “LED 1 off – LED 2 on” 1 second”LED 1 on – LED 2 off ” 1 second…
If we based it on the previous program, we would only need to change the states of the LEDs so that when the first is on, the second is on and vice versa. The solution this time would be:
With this exercise we have finished the session on LEDs. LEDs may appear to be simple components but you will soon see that they have multiple applications. In the next sessions we will use them together with new components to learn how to program with bitbloq. Why not have a go at creating your own lighting effects in the meantime?