Two Sensor

In this scenario, two light sensors are positioned so that thet are on either side of the black line the robot is intended to follow.  By detecting which sensor changes from white to black, we know how to turn back toward and onto the line.  This robot is therefore able to follow any line making any type of turns (left or right etc)

There are 4 possible states which can occur:


L – Black R – White

Robot needs to make a right turn to get back centered onto the line


Both White

In this case the robot is perfectly centered above the black line and can drive straight forward, without corrective action.

Both Black

This is the case when the robot reaches an intersecting other black line. This is a special case and can be used to execute a special action.


L – White R – Black

Robot needs to make a corrective action, in this case it needs to make a left turn to turn back onto the black line.

This can be expressed using the following flow-chart:

Note that in case of the Left and Right Sensor both detecting black, no action is determined.  We could use it as a special condition, to for-example stop driving, or keep driving straigth and in effect ignoring the intersecting line.

Program Example

Things to note:

  1. A Forever loop is used to repeatedly get sensor readings and apply the appropriate steering correction to the wheels, as fast as possible. The loop will repeat several times per second.
  2. Inside the loop, we first set a logic variable to ‘false’ which is used to control the outer loop, and is effeted when the special condition occurs where both the left and right sensor see black — corss an intersecting black line.
  3. We repeatedly check the Left light sensor and then switch based it seeing more then 50% of light (none-black).
  4. We then check the Right light sensor and switch based on it seeing more then 50% of light (none black) The turns are done by stopping one wheel and turning only the other wheel, using individual Motor blocks for each motor. This is more consistent and faster than using a single 2-motor Move block with steering because the rotation sensors don’t interfere with the amount of steering.
  5. When the special condition occurs where both the Right and Left sensor see black, a the Logic Varibale is set to True. whne this occures, the overall control loop is exited.

Motor block duration is set to Unlimited because we just want to start moving that way then immediately loop back and check the sensor again to see if we need to change direction (Unlimited here means “Go this way until I tell you otherwise”).

Download Code Example: [download id=”1″]