Duration – 45 min
Difficulty – MEDIUM
Hello young coder! Would you like to prevent intruders from bugging you when you are learning to code robots? Then this lesson is for you! We will create a robot that will work as an alarm that blinks with LEDs, makes noise with buzzer, a shakes the robot head when an intruder is detected.
- LOFI Brain
- Light sensor
- Distance sensor
- LED x2
- Servo motor
- USB cable
Robot construction – the project is based on ROBOT HEAD construction
- Light sensor → INPUT1
- Distance sensor → DISTANCE port
- Servo motor → OUTPUT1
- Green LED → OUTPUT3
- Red LED → OUTPUT4
- Power supply → USB cable → computer
Turning the alarm on and off with conditional blocks
Lets start with the most basic feature of this robot – turning the alarm on and off, we need conditional IF/ELSE block to do this.
The robot will switch the alarm if the distance sensor detects anything closer than 70cm if front of the robot.
Now we will add operational function of our robot – controlling LED and buzzer. (the LEDs are connected to OUTPUT3 and OUTPUT4).
The next feature to add is turning the alarm off by stroking the robot head. To do this we will use the light sensor on top of the robot head – when you cover it with your hand light level read by the sensor goes down.
It looks great and works almost as a real alarm system. Try to test it for a while, do you find any bugs? Pay attention to situation when the distance sensor detects the intruder (distance sensor reading < 70) an at the same time you try to turn off the alarm by covering the light sensor. The robot keeps blinking and buzzing an we cannot turn the alarm off. If you look at the code you will see that each instruction in the main REPEAT block is executed from top to bottom - the robot starts the LEDs and beeps (intruder!) and then turns of (as you stroke robot head). How to solve this issue? We need a variable.
Using variable to setting the alarm
Lets try to make it this way. The code first checks for intruder and then checks if you are already aware of it (if you are turning of the alarm). We create a variable ALARM. When robot detects an intruder it sets ALARM to TRUE, but if you cover the light sensor at the same time ALARM is then set to FALSE.
You can find TRUE/FALSE block in the category LOGIC. As you can see we use new type of data – logical value – so called BOOLEAN.
This is our alarm control system. To check if it operates properly we will use console to display it`s state (value of ALARM variable). Open INPUTS MONITOR, start the code and test if ALARM state changes as ut should.
If everything works as it should lets once again add robot actions – blinking of LEDs and buzzer beeps when the ALARM variable is true. Once again add conditional IF/ELSE block but this time it`s execution is dependent not on distance sensor reading but the value of ALARM variable.
This conditional blocks holds all the instructions how the robot should act IF the alarm is on (buzer and LEDs turn on) and ELSE if the alarm is off (buzzer and LEDs turn off).
Our alar works great but it is a bit monotonous isn`t it? Lets improve it a bit and make the signals turn on and off repeatedly.
Adding some motion
The last feature we will add is robot`s movement – our alarm will be more scary if the robot starts to shake it`s head as soon as it see the intruder.
As you know head is mounted on a servo motor. Head is directed forward when the servo is set to 50. To make the head shake left and right we have to make the servo switch constantly between positions 40 and 60.
When the alarm is we set servo in similar way as blinking LEDs. Set servo to 40, wait some time, set servo to 60, wait some time…. When the alarm is turned off we want the robot head to set forward so we set servo to 50.
Alarm is ready!
Our robotic alarm is finished. You learned more about conditional blocks and boolean variables.