Line Following: Making It Smoother Pro Preview

So far, we’ve explored a line-following algorithm commonly referred to as duck walking. Let’s take a closer look at how it works and consider how many distinct states it actually involves.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #885
  • 30 Mar 2018

The line-following program that implements the duck walking algorithm looks like this:

Duck Walking Line Following Program

It consists of two basic states:

  • If the color sensor reads black, the robot turns in one direction;
  • If the color sensor reads white, the robot turns in the opposite direction.

As you can imagine, this approach isn’t very efficient. The robot is constantly switching between left and right turns, and never moves straight forward—even when the line is perfectly straight. This makes the movement slow and inefficient. In this example, we assume the robot is following the right edge of the black line.

So, how can we improve this? One idea is to introduce a third state where the robot moves straight ahead. The improved model would have three states:

  • Turning left – when the robot is outside the line:
    Right of the Line - Turn Left
  • Turning right – when the robot is over the line:
    Over the Line - Turn Right
  • Going forward – when the robot is right on the edge of the line:
    On the Edge - Go Forward

To implement this, we need to stop using the color mode of the color sensor and switch to reflected light mode. This mode returns numeric values based on how much light is reflected off the surface under the sensor.

Let’s say we’ve measured the reflected light values in each zone:

  • Over the black line → 9
  • Over the edge of the line → 32
  • Over the white area → 55

Programming the robot to go forward only when it reads exactly 32 isn’t practical. Instead, we allow some margin of error. Let’s say we accept values between 27 and 37 (±5 from 32) as "on the edge". So:

Threshold Diagram

  • If the value is less than 27, the robot is on the black line and should turn right.
  • If the value is between 27 and 37, the robot is over the edge and should go forward.
  • If the value is greater than 37, the robot is in the white area and should turn left.

The full decision logic for the improved algorithm looks like this:

Threshold Algorithm Logic

Try measuring the values your robot reads over the black line, the edge, and the white area. Then implement this three-state algorithm. Break it down into smaller, manageable tasks and test each part as you go.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Instructors Remote Training
  • 183
  • 280:11
  • 156
Image for Competition programming
  • 7
  • 0
  • 8
  • 3d_rotation 1
Image for Robotics with LEGO - Level 2.0 - Robots in Factories
  • 44
  • 15:01
  • 129
Image for Level C1. "Transportation". Robotics with LEGO
  • 33
  • 1:03
  • 110
Image for Robotics with LEGO - Level 2.0 - Robots in Factories
  • 44
  • 15:01
  • 129
Image for Lesson 5 - Robo Car
  • 6
  • 4
  • 11
  • 3d_rotation 1
Image for Build, Code, Play: Your First Adventures with SPIKE Prime
  • 4
  • 0:33
  • 27