Hopefully you’re team has taken my last post and tried to program a line following program themselves. It’s not that difficult if you take some time to think about it.
I’ll outline the program here and you can see if your team’s program is the same, or even better.
Remember that when following a line, you’re actually following the edge of the line. We’ll say we want to follow the right edge. When the robot sees white, it should turn left and when it sees black, it should turn right. You should have realized that this needs a switch statement.
The condition of the switch statement is the mid-way point of what your robot determines is white and black. If you have calibrated your light sensor, this should be pretty close to 100 and 0, respectively. So for the sake of this discussion, we’ll use 50.
Now for the top branch, or white branch, we want to turn left. If you’ve designed your robot using my earlier tip, your left motor will be attached to port B and your right motor will be attached to port C. To turn left, you want to make your right wheel go, while not moving your left motor.
For the bottom branch, or black branch, you want to do the opposite.
Remember that if you do not explicitly turn off a motor, it will keep going. So if the light sensor sees white when it first starts, turns on the right motor to turn left, and then sees black and turns the left motor on to turn right, both motors will be on and it will go straight. So you need to make sure you include a stop motor block to stop the motor you don’t want running.
Now that you have the wheels programmed to do the right things depending on what the light sensor detects, you have to do it more than once. That is accomplished with a loop. Add a loop to the beginning of the program and pull the entire switch statement into it. The loop will expand to accommodate the switch.
By default, the loop goes forever. You can now test your program to see if it follows a line like you expect it to.
To be useful, the loop must be terminated in some way, usually using some kind of sensor. Change the loop’s end condition to a rotation sensor and set the loop to continue until one of the robot’s wheels has traveled 1000 degrees. It should now stop on its own.
Note that any line follower program will only work with the light sensor(s) in front of the drive wheels. If it’s in line with the drive wheels, the light sensor can’t lead the robot enough to direct it.
As I mentioned in my previous post (and as Dean Hystad has aptly pointed out), there are many more ways to follow a line that are much smoother and more robust. But I hope this at least gets your team started. You can just use this line follower as is to complete some missions, or spend some time tweaking it to work better. My teams have used this algorithm before and it’s worked well enough. But our mission accuracy certainly could have been much better with a better algorithm.