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
- #2464
- 18 Jul 2025
The line-following program that implements the duck walking algorithm looks like this:
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:
-
Turning right – when the robot is over the line:
-
Going forward – when the robot is right on the edge of the line:
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:
- 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:
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

Build, Code, Play: Your First Adventures with SPIKE Prime
Welcome to an introduction to the SPIKE Prime set! In this course, you’ll find 7 step-by-step 3D building instructions for SPIKE Prime robots, designed to help you explore the full potential of the base set. Once you've built them, you can bring them to life with our fun programs—these range from simple tasks like moving forward to more complex actions like detecting the nearest object.
And if you're ready for a challenge, we’ve also prepared some advanced tasks! These will encourage you to research and experiment with both the programs and the robot itself. Of course, you're always welcome to add your own creative touches and enjoy the process along the way.
- 4
- 0:33
- 27