Line following: reverse engineered and explained


After reading the question of @kadelglass and the answer of @Gloria, I decided I wanted to understand the proposed script (see Codey Rocky Line follower formula).

I got it working, although it needed some small tweaks for me. I thought I’d share my insights, since I saw other people struggling as well. Disclaimer: there are probably some errors in my explanation, feel free to correct them.


  • speed = how fast do you want the robot to go. This is just a multiplier. Setting it to 10 rather than 70 worked for me.

  • turning radius = the default turn it takes, a necessary bias. 50 is straight and 20 means it’ll go left even on the line. This is to prevent crossing the line. This bias makes sure the robot is always on one side of the line.

  • reflect_low = the lowest reading of the light intensity, this would be when it’s ON the line. Test this and fill in the right score. For me it was 15.

  • reflect_high = same, but reading for everything but the line, probably white. For me it was 95.

  • reflect = the reading of the light intensity as it is going

  • direction = where should the robot go? 50 is straight, lower is go to the left and higher is go to the right. The direction is calculated in the formula, which results in a value between “radius” (default left) and 100 (strong right)
    Next, the speed of both wheels is calculated (I put in an extra step for readability). Both wheels going same speed = straight.

The “repeat until c pressed”: I changed this to “a pressed”, or else the script would stop immediately.
Finally, I added the current reflection in the display, which gives a pretty cool effect.

Below my resulting script. The variables are Dutch (for my children), but you can name them anything you want.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.