Need Help with Line Follower Code

The robot just drives forward and doesn’t follow the line
#pragma config(Sensor, in1, LineFollowerRight, sensorLineFollower)
#pragma config(Sensor, in2, LineFollowerLeft, sensorLineFollower)
#pragma config(Sensor, dgtl1, LeftClaw, sensorTouch)
#pragma config(Sensor, dgtl2, RightClaw, sensorTouch)
#pragma config(Sensor, dgtl3, CallButtonFront, sensorTouch)
#pragma config(Sensor, dgtl6, BInput, sensorSONAR_cm)
#pragma config(Sensor, dgtl8, CallButtonBack, sensorTouch)
#pragma config(Sensor, dgtl10, FInput, sensorSONAR_cm)
#pragma config(Motor, port2, LeftBack, tmotorVex393_MC29, openLoop, driveLeft)
#pragma config(Motor, port4, Claw, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, RightBack, tmotorVex393_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port8, RightFront, tmotorVex393_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port9, LeftFront, tmotorVex393_MC29, openLoop, driveLeft)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
//Code for the Claw
while(1==1)
{
if(SensorValue[LeftClaw]== 1)
{
motor(Claw) = -50;
}
if(SensorValue[RightClaw] == 1)
{
motor(Claw) = 50;
}
if (SensorValue[RightClaw]== 0 && SensorValue[LeftClaw] == 0)
{
motor(Claw) = 0;
}
//Line Follower Code /////////////////////////////
if(SensorValue[CallButtonFront] == 1)//BAFDDFDDD
{
if (SensorValue[LineFollowerRight] <= 1000)
{
//turn left
motor(RightBack)= 0;
motor(RightFront)= 0;
motor(LeftBack)= 20;
motor(LeftFront) = 20;
}
if (SensorValue[LineFollowerLeft] <=1000)
{
//turn right
motor(RightBack)= 20;
motor(RightFront)= 20;
motor(LeftBack)= 0;
motor(LeftFront) = 0;

		}
		//go straight
		if (SensorValue[LineFollowerLeft] && SensorValue[LineFollowerRight] >=1000)
		{
			motor(RightBack)= 20;
			motor(RightFront)= 20;
			motor(LeftBack)= 20;
			motor(LeftFront) = 20;
		}
	}
}

}

Coding help rules:

  1. Format your code. :x: Format your code with ``` on the line before your code and again on the line after.
  2. Use proper indentation. :x:
  3. Ask nicely.
  4. Send your code. :white_check_mark:
  5. Explain what you tried. :x:
  6. Explain what you want. :x:
  7. Explain what’s happening. :white_check_mark:

What block of code is the robot executing, why, and how do you know?

I formatted your code legibly. Your welcome.

#pragma config(Sensor, in1, LineFollowerRight, sensorLineFollower)
#pragma config(Sensor, in2, LineFollowerLeft, sensorLineFollower)
#pragma config(Sensor, dgtl1, LeftClaw, sensorTouch)
#pragma config(Sensor, dgtl2, RightClaw, sensorTouch)
#pragma config(Sensor, dgtl3, CallButtonFront, sensorTouch)
#pragma config(Sensor, dgtl6, BInput, sensorSONAR_cm)
#pragma config(Sensor, dgtl8, CallButtonBack, sensorTouch)
#pragma config(Sensor, dgtl10, FInput, sensorSONAR_cm)
#pragma config(Motor, port2, LeftBack, tmotorVex393_MC29, openLoop, driveLeft)
#pragma config(Motor, port4, Claw, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, RightBack, tmotorVex393_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port8, RightFront, tmotorVex393_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port9, LeftFront, tmotorVex393_MC29, openLoop, driveLeft)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main() {
  // Code for the Claw
  while (1==1) { // Forever
    /* The logic in your code:
    if LeftClaw pressing -> spin claw reverse
    if RightClaw pressing -> spin claw forward
    if neither pressing -> stop claw
    if CallButtonFront pressing ->
      if LineFollowerRight is dark -> turn left
      if LineFollowerLeft is dark -> turn right
      if (LineFollowerLeft) and if (LineFollowerRight is light) -> go forward
    */
    // The most rational thing to think is that LineFollowerRight is always > 1000
    // Do you know it's not? If so, how?
    if (SensorValue[LeftClaw]== 1) {
      motor(Claw) = -50;
    }
    if (SensorValue[RightClaw] == 1) {
      motor(Claw) = 50;
    }
    if (SensorValue[RightClaw]== 0 && SensorValue[LeftClaw] == 0) {
      motor(Claw) = 0;
    }
    // Line Follower Code /////////////////////////////
    if (SensorValue[CallButtonFront] == 1) { // BAFDDFDDD
      if (SensorValue[LineFollowerRight] <= 1000) {
        //turn left
        motor(RightBack) = 0;
        motor(RightFront) = 0;
        motor(LeftBack) = 20;
        motor(LeftFront) = 20;
      }

      if (SensorValue[LineFollowerLeft] <=1000) {
        //turn right
        motor(RightBack)= 20;
        motor(RightFront)= 20;
        motor(LeftBack)= 0;
        motor(LeftFront) = 0;
      }

      if (SensorValue[LineFollowerLeft] && SensorValue[LineFollowerRight] >=1000) {
      // This if is wrong. SensorValue[LineFollowLeft] will almost always evaluate to true
      // You have to do (LineFollowerLeft > 1000 && LineFollowerRight > 1000)
        // go straight
        motor(RightBack)= 20;
        motor(RightFront)= 20;
        motor(LeftBack)= 20;
        motor(LeftFront) = 20;
      } // End go forward if
    } // End if CallButtonFront pressed
  } // End forever loop
} // End task main
5 Likes
  1. what color is your line?
  2. when the line tracker is above the line, what values does it return? Likewise, when the line tracker isn’t on the line, what values does it return?
2 Likes