In this lesson, we use the 2ch Line Follower module on the car to monitor the road condition. The edge of the table was used to simulate the cliff situation. When driving to the edge of the table, the infrared reflection received is 0 and the car stops.
Click Variables category, then click the Create Variable button so as to create four variables: leftVal, rightVal, reference, and speed.
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 part.
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.
Click Variables category, then drag set leftVal to block into Start block. And click the middle drop-down box to change the leftVal to “ reference ”, followed by a number block (0 block).
Similarly, initialize the speed to 30.
Click Variables category, then drag set leftVal to block into Forever block.
Click Modules category and drag 2ch Line Follower A0 get value block to behind the set leftVal to block. Let the read value of the left line follower sensor be assigned to leftVal.
Similarly, assign the read value of the right line follower sensor to the variable, rightVal.
Click Logic category for dragging an if do block to below the set rightVal to block, and click setting icon to add an else block to the area below the if block, which is to change it into if else block.
Note: Click setting icon again to close the pop-up windows.
In the Logic category, drag out an and block to the area behind the if block, and click the drop-down box to select “or”.
Place a Comparison Operator ( = ) block on the left side of the or block; after that, change the value to <. Place a leftVal block under the Variables category on the left side of the or block and a reference block on the right.
Similarly, on the right side of the or block, create a judgment condition: if rightVal is less than reference.
Click into the PiMobile category and drag the Motor set Motor1 speed to 75 block to the do block.
Click on Math category, then drag a square root 9 block to the Motor set Motor1 speed to block, and click on the drop-down box and choose “–”.
Put a speed block after the Comparison Operator (-) block.
Set the speed of Motor2 to -speed as well.
Click Basic category again so as to add a delay block, and change the value to 500. That is, when the front is a cliff, the car go backward for 500m at once.
Likewise, after go backward 500 ms, then let the car to turn left.
If no cliff is detected, let the car keeping moving forward.
So that’s the end of our codes. Save the project and flash it into the Raspberry Pi (refer to lesson 0 for details).