Following Line. How to correct the vehicle and/or make it turn in a intersection

The left and right sensors are outside of the dark surface.

Here's the pseudocode,the explanation is below the code:

prevRight = 0;

prevLeft = 0;


sensorRight = 0;

sensorLeft = 0;

void setup() {
	read the value of sensors and put it
	into variable prevRight,prevCentre and prevLeft



}

void loop() {
	read the value of sensors and put them
	into variable sensorRight,sensorCentre and sensorLeft


	take the difference between the current sensors with 
	the previous sensor

	





}

So imagine there is a path with T-shaped.
I want to make the vehicle to turn left/right at the intersection, but also for the vehicle to correct itself if it's going to out of the track

  1. Turn at intersection
//turn right

if(sensorRight < prevRight && rightDifference >=100) {
      turn right at interseciton
}

I also can use the code above to correct the vehicle, so I need to differentiate between two of them

I tried one solution :

//correct vehicle (same as the code above for turning right at intersection
if(sensorRight < prevRight && rightDifference >=100) {
      correct to the right
}

//turn right at intersection
if(sensorRight < prevRight && rightDifference >=100) && when the left sensor also detect dark surface {
      turn right at interseciton
}

The last block (turn right at intersection) sometimes works sometimes does not work.
I found the cause,it's because the left and the right sensors are not precisely aligned, so sometimes the right sensor detects the darksurface while the left sensor still detects the light surface

I have a lot of experience with middle school students and line following LEGO robots. The first question is strategy. By far the most successful strategy is to follow one edge of the line. Light sensors return a numeric value for highly reflective (white) surfaces and a different numeric value for non-reflective (dark) surfaces. At the edge of the line, the numeric value is halfway between dark and light. If the value read is too "dark", the robot turns a little to the "light" side. If too "light", a turn to the "dark" side is made. For this strategy, only one sensor is needed. You do need some "do nothing" range so that adjustments aren't made too often (aka oscillation.) Intersections are navigated easily, since to the robot it's just a regular turn. There is no choice at a T intersection. The robot will always take either the right or left side, depending on which edge of the line the robot is on.

On occasion, a student attempted to make a two sensor line follower. The strategy here is to straddle the line. The difficulty is turns. How do you distinguish between a slight course correction, a right or left turn, and a T intersection. You can't depend on the alignment of the sensors because you may approach the T at a slight angle and then you have the same issue. For a corner, the strategy could be to continue forward a little to see if it's a corner or a T. If it's a corner, you turn. If it's a T, then what? Random choice? Always go left?

You probably don't need to keep track of the immediately preceding reading unless the reflectity is very variable. If it's highly variable, an average of the last 10 readings may be better. If it's not so variable, picking level ranges is probably good enough.

Good luck.