Duration – 45min
Difficulty – EASY
In this project we will make a simple construction of a lamp with two LEDs that will turn on automatically when it gets dark.
Before starting this lesson build the lamp with our instruction or design your own!
What we need:
- LOFI Brain
- Light sensor
- LED x2
- USB cable
- Light sensor → INPUT3
- Green LED → OUTPUT3
- Red LED → OUTPUT4
- Power → USB cable → computer
Controlling LED modules with “Set OUTPUT” block
At the beginning lets learn how to control LED modules. In LOFI Blocks app go to ROBOT section, tak SET OUTPUT block and put it on the workspace, inside the main REPEAT block. Change the SET OUTPUT parameter to OUTPUT3, duplicate the block (right click -> duplicate) and set the new one to OUTPUT4. Start the program, if everything is connected properly the LED modules should light up.
Try to change the brightness of LEDs, tweak the number value in SET OUTPUT block to anything between 0 and 100. Do you see how it affects the brightness?
Reading data from the light sensor
Controlling the LEDs manually is fun for a while but if we want them to operate automatically we need to add new functionality to our program. Lets use light sensor to detect if it is bright or dark and control our lamp with this input.
First we have to check if our light sensor works properly. Open INPUTS MONITOR by clicking the seccond icon in the upper left menu.
Cover the light sensor with your hand and check how this changes the readings on INPUT3, then try to shine on the sensor with a flashlight – how does this change the readings??
When it is dark (the sensor is covered by hand) it should return readings between 0-10, when it is bright (you shine with a flashlight) the readings should go up to about 50-70 or higher.
PAY ATTENTION – Even if nothing is connected to ports INPUT1, INPUT2, INPUT4 – they seem to return readings similar to INPUT3 (with light sensor) – there is nothing wring with your controller, an input with nothing connected will still return a reading when asked by the computer, it will usually “spy it” from an input nearby – this process is called floating.
Since our robot is able to read light level lets use it in our program. From the blocks category ROBOT pick a block READ and set it`s parameter to INPUT3. Next put the block asa parameter of SET OUTPUT number value as on the picture below.
Now test how your lamp operates in dark and bright conditions.
Our lamp is interactive! It reacts to lightning conditions, but not exactly the way as we would like it. By now the LEDs get brighter when the light level is high and darker when the light level is low. We would like it to work exactly the opposite way. What do we need to make it so? -> A CONDITION
Pick a conditional IF..ELSE.. block form the CONTROL category.
How do conditionals work?
How do we use IF/ELSE block? The light sensor returns number values between 0 (completely dark) and 100 (max bright). Each conditional block as it`s first paramrter has a so called CONDITION STATEMENT (we will get back to it in a while) then comes a section where we put the code that will be executed when the condition is fullfilled (the LEDs turn up) and then a section with code to be executed when a condition is not fulfilled (the LEDs turn off)
CONDITION STATEMENT can be either TRUE or FALSE it cannot be a number. So to make a conditional block react to a light sensor readings we have to convert them from numbers (0-100) to logical (boolean) values (TRUE/FALSE). To do so we will use another block – COMPARISON
You will find arithmetic comparison block in the category NUMBERS, do not let the serious-sounding name intimidate you, the logic behind this block is fairly simple.
This block compares two values (left and right parameters) together. It has four comparison modes.
First option is an EQUAL operator, it checks if two parameters are …equal. Seccond option is NON EQUAL. Third option is MORE THAN and the fourth option is LESS THAN.
In our code we are checking if the left value (light level) is LESS THAN the right value which is a threshold set by us – this time it is 20.
As you remember the light sensor is connected to INPUT3. With the comparison block we check if the light level is less than 20, then the comparison block returns TRUE, otherwise (light level is greater than 20) the comparison block returns FALSE.
The last thing to do is to put the comparison block inside the IF conditional block as the condition statement.
Now our your lamp should work like below – it turns on when the light goes dark and automatically turns off whne the light goes up.