Line Tracking Module with Robit Smart Car
Wouldn't it be cool to have the Robit Smart Car move around on a surface above ground, without it falling off?
In this guide, you will learn to connect and use the line tracking module to the Robit Smart Car. It will be programmed in MakeCode to perform edge detection.
Complete this guide to have a Robit Smart Car that can detect edges above ground.
Step 1 Connect module and motors to Robit motherboard
In this guide, you will learn to use the line tracking module. This module has an infrared sensor which consists of a transmitter and a receiver. While the transmitter emits infrared light, the receiver can detect the infrared light reflected by the ground. Note: When running on black-coloured ground or objects, which absorb infrared light, the receiver will not be able to detect infrared light.
If you haven't done so already, head over to the Robit Smart Car product page for instructions on how to assemble the smart car.
For this guide, we will need:
- The line tracking module is to be connected to the robit motherboard as shown in the first image. Make sure two 3-pin wires are connected to Pin 15 and Pin 16 on the motherboard.
- The two motors are to be connected to M1 and M2.
- Connect the other end of the 3-pin wires to line detection module as shown in the third image
Step 2 Install MakeCode package
First, open MakeCode Editor. To easily program the Robit Smart Car, we'll use the 'robit' MakeCode package over in the Extensions tab:
- Click on 'Advanced'
- Click on 'Extensions'
- Type 'robit' in the search bar
- Click on the robit package and it will be automatically added to the MakeCode editor
Step 3 On start!
The following blocks can be found under the 'Robit' tab.
- Set 'init line follow pin ...' block to J1 (P15, P16) as we connected the line tracking module to Pin 15 and 16 on the Robit motherboard.
- Then get out a 'Left wheel ... speed ... Right wheel ... speed ...' block.
- Remember how we connected the motors? We connected the motor on the left wheel to M1, and the motor on the right wheel to M2. So change this block's values to be 'M1 ... 20 ... M2 ... 20'
Step 4 Create variables!
Click on 'Variables' tab and create two variables. Name one 'right' and the other variable, 'left'.
Step 5 Set variables to '... line follow digitalpin' blocks!
- From the 'Robit' tab, grab a ' left line follow digitalpin' block and 'right line follow digitalpin' block.
- Assign the 'left' variable to 'left line follow digitalpin'
- Asign the 'right' variable to 'right line follow digitalpin'
Step 6 Set a condition!
- From the 'Logic' tab, get out an 'if ... then ... else' block.
- Get out a 'not' block and a ' ... or ... ' block.
- Arrange them so that the condition reads 'if not right or left then'.
What this means is if the right and left variables are '0' then do the following (within the if block). So, if the right or left variables are '0', this means the tracking module's infrared sensor has detected edges. To prevent the Robit Smart Car from falling off the edge, we'll set the speed of the motors to be negative; this causes it to reverse.
- So get out another 'Left wheel ... speed ... Right wheel ... speed ...' block, then change its values to be 'M1 ... -20 ... M2 ... -20'
- Add a 'pause (ms)' block and change its value to 500.
Step 7 Add an if block inside the if block!!
Next, we'll create another if condition within the existing if condition.
- So, get out another 'if ... then ... else ... ' block.
- Click the white plus icon to add an 'else if' block.
- Press the white minus sign to delete the 'else' block for this newly created condition.
- We'll just need the 'if' and the 'else if' here.
After the smart car detects an edge, and it reverses, let's get it to either complete a right or a left turn. We'll use a randomly generated number to determine whether the smart car should complete a right or a left turn.
- Create a new variable and name it, 'num'.
- From 'Math' tab, get out a 'pick random ... to ...' block. Set its values to be '0 ... 100'.
- Next, get a ' ... < ... ' block from 'Logic' tab.
- Then get a 'num' block from 'Variables' tab and place it within the ' ... < ... ' block so it reads 'if num < 50 then'.
- Next, from 'Robit' tab get out a 'Motor Stop M1' block.
- Place a 'pause (ms)' block after it with a value of 300.
- Get another '... < ... ' block, this time changing it to ' ... > ... '. Place it within 'else if' so it reads 'else if num > 50 then'
- Use another 'Motor Stop ... ' block but for the motor attached to M2 this time. Add a 'pause (ms) ...' block after it, setting its value to be 300.
Step 8 Complete MakeCode!
Now that we've programmed the conditions for when an edge is detected, what about if the infrared sensor does not detect an edge? To complete the MakeCode, we'll focus on the 'else' condition.
- Add another 'Left wheel ... speed ... Right Wheel ... speed' block.
- Set it to read 'Left wheel M1 speed 20 Right Wheel M2 speed 20'
If the infrared sensor in the tracking module does not detect an edge, motor speed is set to 20. This means the smart car will continue to move forward.
Step 9 Upload to micro:bit
To upload the code to the micro:bit, follow these steps:
- Connect the micro:bit to the computer via microUSB cable
- Click on the 'Download' button and the hex file will be automatically downloaded to your 'Downloads' folder
- Drag and drop the downloaded .hex file to the MICROBIT drive
- Leave the micro:bit alone for a few seconds as it blinks. The code is uploading!
Try powering the Robit Smart Car with the battery pack now with the micro:bit plugged in. When placed on a flat surface above ground, it will avoid falling off.
There is an exception! Try placing it, powered and running, on a black coloured table or object. Be prepared to catch it!
As mentioned in the first step, when encountering black ground or other objects that absorb infrared light, the receiver can't receive infrared light.