Duration – 45 minutes
Difficulty – easy
Lets make a gate that opens automatically when something arrives and learn how to use conditional instructions, servo motors, distance sensor and LEDs.
- LOFI Brain
- Servo motor
- Distance sensor
- LED module x2
- USB cable
- Distace sensor → Distance port
- Servo motor → OUTPUT1
- Green LED → OUTPUT3
- Red LED → OUTPUT4
- Power supply → USB cable → computer
Opening and closing the gate
First thing we will add to our program is the most basic function – controlling the movement of the gate. You have to note one thing – during construction each time servo motor is set slightly different so we have to find a numerical position at which the gate will be “closed” and “open”. Take a block that controls the servo motor – SET SERVO from ROBOT section put it in the main REAPEAT block and starte the program.
Try setting the servo position manually and find two values one where the gate is set horizontally (closed) and seccond where the gate is set horizontally (open). Note this two values we will use it in a while.
Now lets add some logic to our program to do this we have to use IF/ELSE conditional block, you will find it in the CONTROL category.
First section will contain functional blocks executed when the gate is open put there SET SERVO block with a value you found before for the gate set vertically. In our case it is SET SERVO OUTPUT1 to 100, but remember that 100 value in your case might be a different number.
Seccond section contains blocks executed when the gate is closed.
The next step is setting the condition statement which will define eheter the gate has to be open or closed.
Our main tool to control the gate is a distance sensor which returns measurements in centimeters of objects that are in front of it.
When the distance sensor detects no objects it returns a value of 100, the gate has to be closed. When a car arrives at the gate distance sensor detects it and returns a measurement of about 20cm this is a signal for a gate to open.
So we know that the distance sensor returns measurement values – numbers. But we have to make a logical statement out of it – true or false. To convert a distance value into a logical value we have to use arithmetic comparison block, you will find it in category NUMBERS
On the left side of comparison we put DISTANCE SENSOR block – this is our distance measurement. On the right we set a numerical value, we decide at which distance we will trigger opening of the gate – this time it will be 30cm.
We set comparison mode to LESS THAN
Great! the gate opens when a car is detected… but if you test it for a while you will find that immediately after the car moves away from the distance sensor field of view the gate closes and it makes it quite hard to pass through the gate. To fix it lets add a delay of 3 seconds before the gate closes.
It is XXI century. Lets improve our gate a bit more and make in speak – “The gate will close in three seconds”
In the SPEECH category you will find two blocks – LANGUAGE will set the speech synthesizer language to the one we like – which is ENGLISH in this case. The SAY block will make speech synthesizer speak a sentence provided as a parameter of this block.
PAY ATTENTION – when speech synthesizer is talking the code doesn`t pause it`s execution it is good practice to put a delay block after SAY to payse the program execution until the speech synthesizer finishes saying what it has to say
LEDs – light signals
The last improvement to our gate is adding light signaling. When to gate is closed red LED (OUTPUT4) is turned on which means STOP. When the gate opens the red LED turns off and the green one (OUTPUT3) turns on, which means GO. We control the LEDs with SET OUTPUT block from ROBOT category.
Our automatic gate program is finished!