Explorer: Follow line
The lesson explains how the eXperiBot uses a sensor as a Line follower to follow a predetermined line independently.

Learn how your eXperiBot can follow a line.
Model solutions
Unit 1
No program code is required for this unit. You just need to make the eXperiBot ready to start, which means:
Assemble the eXperiBot completely
Establish a connection to the app
Draw or stick a black line on the floor
Unit 2
First, attach the Multisensor to the front bottom of the eXperiBot. The Multisensor’s module LED should then light up blue.
Solution file to download and import into the eXperiBot Blockly app.
Unit 3
Solution file to download and import into the eXperiBot Blockly app.
Unit 4
Solution file to download and import into the eXperiBot Blockly app.
Lesson contents
Unit 1
Connect your eXperiBot to the app.
Proceed as follows:
Switch on your eXperiBot.
Then press the “Connect” button at the top right of the screen. Follow the instructions to connect your eXperiBot to the app.



You’ve assembled your eXperiBot and connected it to the app—now you’re ready to go!
Show how you want your eXperiBot to drive—make your own line!
Take a black pen. Draw a straight line on a large, light-colored piece of cardboard. The line should be at least 3 cm wide and 50 cm long.
Or: Take black adhesive tape and stick a line on the floor.
Now your eXperiBot can start! Program your eXperiBot and show what it can do!

Unit 2
Your eXperiBot needs sensors to function like a robot in a shopping center. These sensors help it recognize its surroundings and respond appropriately.
In the “Avoid obstacles” lesson, you already got to know the Multisensor. You learned how it recognizes objects.
This is how your eXperiBot can avoid obstacles.
The following module is called Multisensor.

But the Multisensor can do even more: it can also detect a black line.
The Multisensor in your eXperiBot can detect whether the surface is light or dark.
This feature allows your eXperiBot to distinguish whether it is driving on the dark line or on the light surface.
Here’s how it works:
An LED in the sensor emits infrared light (IR) onto the floor:
Light surfaces reflect a lot of light.
Dark surfaces reflect little light.
The IR sensor measures how much infrared light is reflected back.
This allows your eXperiBot to reliably detect the line.

Status LED
Illumination LED and sensor unit with infrared, color, and ambient light sensor
Infrared sensor on the right
Infrared sensor on the left
Attach the Multisensor to the front bottom of the eXperiBot in the direction of travel.

For your eXperiBot to follow a line, the Multisensor must constantly check: Am I still on the line?
The Multisensor has two infrared sensors (IR sensors):
one for the left side,
one for the right side.
This allows your eXperiBot to recognize three situations (a to c):

For each situation, consider which direction your eXperiBot should travel to follow the line.
If both infrared sensors detect the line, your eXperiBot is completely on the line.
The following setup lets your eXperiBot recognize the three situations:
a) If both IR sensors see the dark line, your eXperiBot drives straight ahead.
b) If only the left IR sensor sees the dark line, your eXperiBot turns left.
c) If only the right IR sensor sees the dark line, your eXperiBot turns right.

In order for your eXperiBot to respond correctly in situations a, b, and c, you need the “if—do” block from the “Logic” category.
What is an “if-do” block?
This block only executes statements if a certain condition is true.
This is called a conditional block.
With a conditional block, you can control the flow of your program using conditions.
A condition can only be true or false.
If the condition is “true”, the program executes the statements; if it is “false”, the program skips them.
This feature lets your eXperiBot respond appropriately to different situations.
When do you need the “if—do” block?
Use it whenever you want your eXperiBot to respond differently in a situation:
If the condition is true, your eXperiBot executes the statement.
If the condition is false, your eXperiBot does not execute the statement.
How do you insert the “if—do” block?
Drag the “if—do” block from the “Logic” category into your program.

Top: Condition
Inside: Statement
Drag the block into the editor and connect it to the “start program” block.

Situation a: If both IR sensors detect the dark line, do the following: Drive the motor forward.
Your eXperiBot only drives forward along the line when both IR sensors detect the dark line. To do this, both IR sensors must be on the dark line. (Situation a)
Your eXperiBot should only drive when both IR sensors see the line. To make this work, you need a condition that checks both IR sensors, so use the “and” block.
In computer science, the “AND” operator links two conditions.
This logical connection is called a logical AND.
Both conditions must be true for the “if—do” block to execute the statement.
Example: If the left IR sensor AND the right IR sensor detect the dark line, then “motor drive forward”.
Drag the “and” block from the “Logic” category into the “if—do” block. Place it in the field for the condition.

Situation a: If both IR sensors detect the dark line, drive the motor forward.
Drag two matching conditions from the “Multisensor” category into the “and” block.
The conditions should match situation a: both IR sensors detect the dark line.
From the “Motor” category, add the “Motor drive forward” statement to the “do” section. This tells your eXperiBot what to do when it detects the line.
Then adjust the speed and duration.
Place your eXperiBot at the start of your line and test your program.
Make sure that both IR sensors on the multisensor are aligned with the line at the start.

Your eXperiBot will now only move forward when both IR sensors detect the line.
What happens if your eXperiBot does not recognize the line?
If the left or right sensor is no longer on the line, your program must also detect situations b and c.

To ensure your eXperiBot responds correctly to situations b and c, add more conditions to your program.
You can expand the “if—do” block by inserting “else if” statements.
Your program checks the conditions from top to bottom. As soon as a condition is true, it executes only that statement and then stops checking.

Help your eXperiBot become a Line follower!
First, look at the code for situation a. Then set up the conditions for b and c correctly.
Finally, insert the appropriate blocks from the “Motor” category.
When the left sensor detects light, your eXperiBot must move to the right. This allows it to find the line again.
Your program could look like this:
If you want your eXperiBot to drive along the line indefinitely, you must repeat this entire sequence infinitely.
Drag the appropriate block from the “Loops” category into the editor.
In the “Drive back and forth” lesson, you already learned about the Loop that repeats a Sequence over and over again. This Loop allowed your eXperiBot to drive back and forth between two stations without interruption.
Test your program.

Try out how your eXperiBot behaves when you change the values for
time,
rotation angle, or
speed in the blocks from the “Motor” category.
Test your program after each change.

Observe closely how your eXperiBot behaves. What do you notice?
Which values work particularly well?
Your eXperiBot follows a line.

Unit 3
Currently, your eXperiBot repeats the commands to be a line follower indefinitely. This means that your eXperiBot constantly checks whether it is still on the line.
Now your eXperiBot should stop and play a melody as soon as it reaches the end of the line. To do this, both IR sensors must detect the white surface.
Replace the “repeat forever” loop with a loop that stops when both IR sensors detect the light-colored surface.
Your eXperiBot continues to drive as long as one IR sensor can still see the dark line.
Your eXperiBot only stops when both sensors can no longer detect a dark surface.
Did you choose the right Loop?
You can replace the “repeat forever” loop with this loop:
Knowledge update “repeat-while” block
What is a “repeat-while” block?
The “repeat-while” block is a Loop. It repeats the statements in the block over and over again as long as the condition is true.
Before the loop starts, the program checks the condition.
If the condition is true, the loop begins and the statements are executed.
If the condition is false, the loop does not start.
Computer scientists call this loop a head-controlled conditional loop because the condition sits, so to speak, “at the head” of the loop and decides whether it runs or not.
When do you need the “repeat-while” block?
Use the block when you need to repeat something as long as a certain condition is true.
How do you insert the “repeat-while” block?
Drag the “repeat-while” block from the “Loops” category into your program.

Insert the condition to be checked.
Place all the statements that your eXperiBot should execute in the loop block as long as the condition is true.
Insert a suitable condition in the “repeat-while” block.
This will stop your eXperiBot as soon as it is no longer on the line. In other words, when both IR sensors no longer detect a dark surface.
If the condition is true, the “repeat-while” block executes all statements.
For the condition to be true, at least one of the two IR sensors must detect the dark line.
In computer science, you use the logical operator “or” for this purpose.
This block allows you to connect two conditions together.
The “or” block returns true if at least one of the two conditions is true.
This means that it is sufficient for one IR sensor to detect the dark line. Then the condition remains true and the loop continues to run.
If the condition in the “repeat-while” block becomes false, the loop stops.
Then your eXperiBot has reached the end of the line. At that point, both IR sensors no longer see a dark background.
To make it easy to recognize when the Loop stops, your eXperiBot should play a sound.
Now complete your program: Add a suitable block that plays a sound.
The new block for playing a sound does not belong in the loop.
The block is only executed after the loop has stopped.
Test your program now. Place your eXperiBot at the beginning of the line and start the program.

Observe whether your eXperiBot follows the line and stops at the end of the line.
Your eXperiBot stops at the end of the line and plays a sound.
Unit 4
Currently, you are using the “repeat-while” block in your program. This block executes statements as long as the condition is true.
Now change the block to “repeat-until”.
Now adjust the condition in the “repeat-until” block so that your eXperiBot can continue to follow the line.
The “repeat-until” block executes statements until the condition becomes true.
Test your program.

Does your eXperiBot still drive along the line just as well? Does it stop at the end of the line as before? This is how it should work if you use “repeat-while” instead of “repeat-until”.
If not, check your condition.
Important: The “repeat-until” block repeats the statements until the condition becomes true.
If you use “repeat-while”, your program will continue to run as long as the condition is true.
For example, as long as an IR sensor detects the dark line.
If you use “repeat-until”, your program will continue to run until the condition becomes true.
For example, until both IR sensors see the light background.
If your eXperiBot follows the line and stops at the end, you have used the “repeat-until” block correctly.
For the “Line follower” program, you can use either the “repeat-while” or the “repeat-until” loop.
Bonus unit 1
Extend your black line with a few curves. Make sure that the curves are not too tight so that your eXperiBot can drive them easily.

Place your eXperiBot on the line and start your program.

Observe: Does your eXperiBot remain reliably on the line even when cornering?
If not, adjust your program or your line accordingly.
If your eXperiBot veers off the line when turning:
reduce the speed,
decrease the turning angle for “Motor turn left” or “Motor turn right”,
make the line slightly wider,
and don’t draw the curves too tightly.
Your eXperiBot follows a curved line.

Bonus unit 2
Use your program to make your eXperiBot act as a line follower and stop at the end of the line.
Modify your program so that your eXperiBot stops at the end of the line for a few seconds and then turns around on the line.
Now repeat these statements continuously.
To do this, insert a suitable Loop into your program.
Test your program.

Observe whether your eXperiBot stops at the end of the line, turns around, and follows the line again.
Adjust your code if something is not working properly.
Your eXperiBot stops, turns around, and drives back along the line—again and again.
