Advanced Lesson 2 Line Following

Share for us


In this lesson, we will learn how to program the car to move along the line on the ground.

Programming Steps

Step 1

After create a new project, click Variables category, then click the Create variable button, and input “reference”.

Step 2

Click the Simulation icon on the left of the page to enter the Simulation page.

Step 3

In the left module category, drag the 2ch Line Follower module to the right area.

Step 4

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.

Step 5

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.

Step 6

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.

Step 7

Click the setting icon in the top left corner and drag an input name block into the inputs block to add a function variable.

Step 8

Then drag an if return block from Function category into the Follow line Function block to create judgment conditions.

Step 9

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.

Step 10

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.

Step 11

So here we get our functions done. There appears one more Follow line with block in Function category.

Step 12

Place an if do block in Forever block, and click setting icon to add an else if block.

Step 13

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.

Step 14

Drag 2 Motor set Motor1 speed to block from PiMobile category into do block , then change the second Motor1 to Motor2.

Step 15

Set the speed of Motor1 to 10 and that of Motor2 to 0 to let the car turn left.

Step 16

Duplicate the Follow line with block and put it behind the else if block, change the A0 to A1.

Step 17

Set the speed of Motor1 to 0 and that of Motor2 to 10 to let the car turn right.

Step 18

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.