PROGRAM WORKSHEET 21: Program to follow a black line

Projected outcome: Pupils learn to use the Mindstorms interface to build a simple program which uses a light sensor to get a model to follow a line on a flat surface. The program could be used to get a hound to chase the Wild Boar of Ben Bulben, or to get the wolf to chase Little Red Riding Hood.

What it does: This program can be used to make a Lego model follow a black line using a light sensor. (See PROGRAM WORKSHEET 22 to find out how the RCX counts events using the touch sensor)

Equipment: RCX, Lego model with two motors independently driven, one light sensor, sheet of paper with circuit done in black and computer running Lego Mindstorms V.2.

Method: Build a model with motors driving wheels mounted on separate axles under centre of model so that model "wags" from side to side when motors are turned on alternately. Use a castor to support the heavy tail and allow for side-to-side movement.



A program starts with this icon. Note that the default name for a program is Untitled. To give it a name of our choice we go to File, Save As, type a name and save it. We can load the program by going back to File, and typing Open to show the folder in which it was saved.

To do: Save this program as light1, and check if its name changed from Untitled.


Setting up the program for the light-sensor

1. On the left panel, click Repeat, select Repeat Forever and drag it to dock under the program start block.

Note that this block has a place-holder at its centre. This block is used to loop an instruction or list of instructions when placed between the words Repeat and Forever.

2. Click Yes or No on the panel on the left and drag into place inside the Repeat Forever.

3. Click the small tab beside No, click Light icon, click Next, click radio button 2, click Next, click Bright, click Next, click radio button Manual, and type in 42.

4. Click Done.


5. Click Small Blocks on the panel on the left, select Power, and drag On For  to place-holder under Yes. Click its small tab, click the check marks over B and C and set the time to 0.1 seconds. Click OK.

6. Repeat procedure to place On For under the No, except we want to remove the check marks over A and B.



7. On the left panel, click Sensors, select Light Sensor and drag it up beside the program start block.

 When its menu opens, select Port 2, and click Next and/or Done from then on.

The light sensor now docks using its own Lego-like strip.

Your program should now display like the image on the left.



8. Click File, Save on the menu at the top of the screen.

9. Click Download to download the program to the RCX. (If you wish to choose a channel to download it to, click Settings and select the channel of your choice.)

10. Place the model with the light-sensor over a black area of the track. Click View on the RCX twice (or until the arrow on the display cycles around to Port 2) to display a reading for black. If it's well below 42 we are OK so far. If not, we need to choose another number at step 3 above.

11. Place the model with the light-sensor over a white area. The RCX will now display a reading for white. If it's well above 42 we are OK.  Press Run and the model will follow the black line.


(move mouse over image to start video) Download directly