Use the two line sensors to tell which side the line is on, and steer to stay on it.
In many warehouses and hospitals, robots carry boxes and medicines along coloured lines on the floor. They do not know where they are; they only know whether each sensor still sees the line.
Under the front of the robot are two line sensors, a little to the left and a little to the right. Each one shines light at the floor and measures what bounces back: a dark line reflects less than the floor around it. In blocks, robot detect line left is true when the left sensor is over the line. You find it in Robot.
The simulator's arena has a dark loop to follow. If the robot does not start on the line, drag it onto the line, facing along it.
Make your prediction first. Help opens once you've locked it in.

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.detectLine(RobotLineDetector.Left)) {
robot.motorTank(10, 40)
} else {
robot.motorTank(40, 10)
}
})
robot motor tank 10 40 steers left (the right wheel is faster). 40 10 steers right.
Watch the robot go round the loop. Look closely at how it moves: smoothly, or in small swings?

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.detectLine(RobotLineDetector.Left)) {
robot.motorTank(10, 40)
} else {
robot.motorTank(40, 10)
}
})
This version uses both sensors, so the robot knows which way it has drifted. It drives straight while both see the line, and steers only when one slips off. Both see the line is an and block (in Logic): it is true only when the question on each side is true.

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
robot.motorTank(40, 40)
} else if (robot.detectLine(RobotLineDetector.Left)) {
robot.motorTank(10, 40)
} else if (robot.detectLine(RobotLineDetector.Right)) {
robot.motorTank(40, 10)
} else {
robot.motorStop()
}
})
Match each case to the right move. Watch the robot in the simulator to check.
Kai's program:

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
robot.motorTank(40, 40)
} else if (robot.detectLine(RobotLineDetector.Left)) {
robot.motorTank(40, 10)
} else if (robot.detectLine(RobotLineDetector.Right)) {
robot.motorTank(10, 40)
} else {
robot.motorStop()
}
})
Match what the sensors see with what the robot should do.
Delivery robots go faster where the line is straight. Make the robot drive at 60 when both sensors see the line. Does it still get round the bends?
Change the first robot motor tank (both sensors see the line) to 60 60.
When neither sensor sees the line, the robot stops. Make its lights turn red too, so a person knows it needs help.
robot set color is in Robot. Put it in the last else, after robot motor stop, and choose red.
Go faster on straight sections: 60 when both sensors see the line.
This ticks by itself when your blocks do what is asked.
Turn the lights red when the line is lost.
This ticks by itself when your blocks do what is asked.
Make a delivery robot: it follows the loop, but if something is on the line ahead, it stops and waits until it is moved.
Add one more question at the very top of the if: robot obstacle distance < 10. Its move is robot motor stop.
Click + on the if to add another else if, then drag the questions so the distance one comes first.
A delivery robot: follow the line, but stop if something blocks the line ahead.
This ticks by itself when your blocks do what is asked.
For a real track, use black electrical tape (about 2 cm wide) on a light floor or a large sheet of paper. Things to notice:
Students follow a line with one sensor (swinging along its edge), then two sensors (drive straight, steer towards the side that still sees the line), and combine line following with the distance sensor to make a delivery robot that stops when blocked. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
The robot has two line sensors under its front. Each is true when it is over the dark line. With one sensor the robot can only swing back and forth along the line's edge. With two, it knows which way it drifted: if only the left sensor still sees the line, the line is to the left, so it steers left (right wheel faster: robot motor tank 10 40). Steering the wrong way is the common slip (Kai's robot).
The simulator's sensors are on/off only. Real sensors measure how much light comes back, which allows smooth steering; that is a good teacher-led extension on real robots. The simulator also adds a small "line assist": after the robot loses the line it slows down for a moment, which helps it find the line again.
The Make combines lessons 3 and 5: the distance question must come first, or the robot would follow the line straight into the box. If the robot does not start on the line, students drag it onto the line, facing along it. MOVE Motor classes can use Kitronik's line-sensor blocks on the real robot; they give a number, so steering on the difference between left and right is a natural extension.
Steering towards the sensor that still sees the line. Ask which side the line is on when only the left sensor sees it.
Stretch: Count laps: when both sensors lose the line for a moment at a gap, or each time a button is pressed, add 1 and show it. Or: drive the loop the other way.
Support: Give the two-sensor program with the turns removed from the middle branches; students choose the steering for each.
Next unit: Modes. One robot, many jobs: a variable chooses between stop, drive and follow. Real-world links: warehouse robots, hospital delivery robots, robot lawn mowers that follow a boundary wire.