3 Line Tracker Program Problem

I have students who are using a 3 line tracker. The code they have written works for the right and left side, but the middle line sensor won’t work. I have double checked the ports and motor set-up. They are set up for line sensors. Here is the code that we have been using.

#pragma config(Sensor, in1, leftlinesensor, sensorLineFollower)
#pragma config(Sensor, in3, rightlinesensor, sensorLineFollower)
#pragma config(Sensor, in4, middlelinesensor, sensorLineFollower)
#pragma config(Sensor, dgtl6, button, sensorDigitalIn)
#pragma config(Motor, port1, leftmotor, tmotorVex393_HBridge, openLoop, driveLeft)
#pragma config(Motor, port10, rightmotor, tmotorVex393_HBridge, openLoop, reversed, driveRight)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

while(1==1){

if(SensorValue(button) == 0)
{
motor[leftmotor] = -127;
motor[rightmotor] = -127;

}

if(SensorValue(middlelinesensor) > 505){
motor[leftmotor] = 100;
motor[rightmotor] = 100;

}

if(SensorValue(leftlinesensor) > 505){
motor[leftmotor] = 127;
motor[rightmotor] = 0;

}

if(SensorValue(rightlinesensor) > 505){
motor[leftmotor] = 0;
motor[rightmotor] = 127;
}

}
}

Look at these two: “middle” and “left” line sensors and lets just assume both >505.
The code will run both statements since both are true. Therefore, the 2nd “if” statement kills the 1st one as soon as it starts and you will never see it run.

I advise using “nested if” statements or my preferred choice “if/else if/ else” statements. like below

if(SensorValue(middlelinesensor) > 505){
motor[leftmotor] = 100;
motor[rightmotor] = 100;

}
else if(SensorValue(leftlinesensor) > 505){
motor[leftmotor] = 127;
motor[rightmotor] = 0;
}
else if(SensorValue(rightlinesensor) > 505){
motor[leftmotor] = 0;
motor[rightmotor] = 127;
}

That way only 1 of the 3 will run at any cycle.

Thank you for your help, we have another problem. The left drive is constantly spinning and the sensors are not repsonding. This is our first year of doing any coding minus the preloaded code so we are having trouble figuering out what our glitches are.

If you’ve changed your code since your first post, you will need to show or tell what you’ve changed. And for readability’s sake, if you highlight your code after pasting then hit the code button, marked with </>, above the post composition box it will wrap it in code tags and preserve the formatting:

code {
    using code tags
}

You’ve got at least three forum threads on this same topic. It helps if you create a single thread the community can respond to. See my post in one of your other threads.