Review Distance Sensor Code

My team has recently started using the V5 distance sensor on our robot, the code we have is supposed to be stopping the robot when it gets close to the goal, the problem is that it doesn’t seem to be working. The robot continues to drive forward and the distance sensor measurements are correct.

Here is the code:

//lma = leftMotorA and rma = rightMotorA and so on
motor_group Drive(lma, lmb, lmc, rma, rmb, rmc);
 Drive.spin(fwd);

 if(distanceSensor.objectDistance(mm)< 15){
   Drive.stop();
 }```

try turning the if statement into a while loop in the form of:

while(distanceSensor.objectDistance(mm)< 15) {
// drive statement here
}
// drive stop here

Or else the if statement will execute once and move on (assuming this is autonomous)

5 Likes

Usually, if there is an error in the code, it is somewhere else. You need to post the whole code for others to help.