Skip to main content

Line Sensors

Sample Code

We have used the device set up to add the 3 wire device of class LineTracker. Two are present named LineL and LineR. for the sensors that are positioned on the Left and Right side of the robot. This code will cause a robot to line up on a line that is perpendicular to the direction of travel. This can be used as a reset to get the robot aligned straight. The values of percent reflection will need to be tuned for the specific application.

void lineUp(int speed = 30){
 float rightLine=LineR.value(percent);
 float leftLine=LineL.value(percent);
 int lspeed=speed;
 int rspeed=speed;
  while (rightLine<=10 || leftLine<=10){
    drive(lspeed, rspeed, 10);
    leftLine=LineL.value(percent);
    rightLine=LineR.value(percent);
    if(leftLine>10) lspeed=0;
    if(rightLine>10) rspeed=0;
  }
  drive(0,0,0);
}