Middle Line Sensor Issue

I have a student working with Line Trackers…we where able to get them all to work on either the right or left side, but it won’t work in the middle. I have looked at the code so much I can’t see the problem. I have put the code in for anyone who would have time to take a look. Thanks!

#pragma config(Sensor, in1, sensorRight, sensorLineFollower)
#pragma config(Sensor, in4, sensorCenter, sensorLineFollower)
#pragma config(Sensor, in8, sensorLeft, sensorLineFollower)
#pragma config(Motor, port1, rightmotor, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port10, leftmotor, tmotorVex393_HBridge, openLoop, reversed)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
wait1Msec(2000);

int threshold = 2048;

while(true)
{
if(SensorValue (sensorRight) > threshold)
{
motor[leftmotor] = 63;
motor[rightmotor] = 0;
}

 if(SensorValue (sensorCenter) > threshold)
 {
   motor[leftmotor] = 63;
	 motor[rightmotor] = 63;
 }

if(SensorValue (sensorLeft) > threshold)
{
motor[leftmotor] = 0;
motor[rightmotor] = 63;
}

// else;
// {
// motor[leftmotor] = 63;
// motor[rightmotor] = 63;
// wait1Msec(0050);
// stop();
//}

//if((SensorValue (sensorLeft) < threshold) && (SensorValue (sensorRight) < threshold))
//{
// motor[leftmotor] = 63;
// motor[rightmotor] = 63;

//}
}
}

Perhaps since the center sensor is presumably in the center of the robot there is less light hitting the tape, and thus less making it back to the sensor? Try having a different threshold value for each of the line sensors.

As far as the program is concerned, to avoid massive swerving back and forth it might be worth it to change the left and right if statements into if else if statements with an else statement at the end setting the motors straight.

Given that you’re using three consecutive if statements with no else controls, my bet is that the center sensor is close enough to the left sensor that they are triggering at roughly the same time, and because the check for the left sensor comes after the center sensor, it takes precedence on what the motors end up doing next.

Change your three if statements to an if...else if... else if construction where the center sensor is checked first and you might have better results.

1 Like