The NXT Light Sensors (#9844) we bought from BrickLink finally came (they came rather quick from Canada, really), so after testing them out (involving Duncan playing with the super-annoying test program which changes the pitch of the piezo speaker inside the NXT brick depending on the amount of light detected) we decided to build a line following robot.

The code to follow a line is pretty easy:

task main() {
    SetSensorLight(IN_1);
    SetSensorLight(IN_4);

    while (true) {
        Wait(50);
        int sensor_left = Sensor(IN_4);
        int sensor_right = Sensor(IN_1);

        if (sensor_left < 50 && sensor_right < 50) {
            OnFwd(OUT_C, 25);
            OnFwd(OUT_A, 25);
        } else if (sensor_left < 50) {
            OnRev(OUT_C, 25);
            OnFwd(OUT_A, 25);
        } else if (sensor_right < 50) {
            OnFwd(OUT_C, 25);
            OnRev(OUT_A, 25);
        } else {
            OnFwd(OUT_C, 25);
            OnFwd(OUT_A, 25);
        }

        // debug output
        ClearScreen();
        TextOut(0, 0, "Left:");
        TextOut(0, 20, "Right:");

        NumOut(40, 20, sensor_right);
        NumOut(40, 0, sensor_left);

    }
}

Unfortunately, it doesn't work on the test pad that came with the NXT set - the wheels couldn't grip on it no matter what I tried!

It was far too warm to drive out to Bunnings simply for some masking tape, so I scrounged in the van to see what I had and found an old roll of white electrical tape, with which we marked out a circuit on the floor. Rather than recompile the program to follow a light line instead of a dark line, I simply swapped the two motor wires over which had the desired effect (I'm not sure if that indicates there's a bug in my program or not?).

Horsham, VIC fwaggle

Published:


Modified:

Never

Filed under:


Location:

Horsham, VIC

Navigation: Older Entry Newer Entry