In this lesson, we will learn how to program the car to move along the line on the ground.
After create a new project, click Variables category, then click the Create variable button, and input “reference”.
Click the Simulation icon on the left of the page to enter the Simulation page.
In the left module category, drag the 2ch Line Follower module to the right area.
Connect SIG1 of 2ch Line Follower to A1 port, SIG2 to A0 port, and then connect 3V3 and GND to 3.3V and GND of Robot HAT.
Go back to Programming page, then click Variables category, and drag a set reference to block into Start block. Moreover, add a number block and change the value to 300.
Build a function to judge if the black line is recognized. Click Function category to drag a to do something return block to the right area, and click do something to change the name to Follow line.
Click the setting icon in the top left corner and drag an input name block into the inputs block to add a function variable.
Then drag an if return block from Function category into the Follow line Function block to create judgment conditions.
After clicking on Logic category, you need to drag a Comparison Operator ( = ) block to behind if and then change the equal sign to less than sign. Put an x block in the Variables category on the left and a reference block on the right.
Get 2 true blocks from the Logic category to put them in the area behind the two return blocks. Moreover, change the second true to false as shown. So, if x<reference, return true, otherwise return false.
So here we get our functions done. There appears one more Follow line with block in Function category.
Place an if do block in Forever block, and click setting icon to add an else if block.
In the Function category, drag the Follow line with block to the end of if block, and then click the Modules category, you will find 2ch Line Follower get A0 value block and put it after x.
Drag 2 Motor set Motor1 speed to block from PiMobile category into do block , then change the second Motor1 to Motor2.
Set the speed of Motor1 to 10 and that of Motor2 to 0 to let the car turn left.
Duplicate the Follow line with block and put it behind the else if block, change the A0 to A1.
Set the speed of Motor1 to 0 and that of Motor2 to 10 to let the car turn right.
So here we’ve written our codes. Save the project and flash it into the Raspberry Pi (refer to lesson 0 for details). Draw a line with a marker or black tape, and the car will follow the line moving forward.