Turning in autonomous

I was trying to program an autonomous turn using encoders and succeeded, but after the motors stopped the robot continues to turn. I tried to fix this by having after it was greater than the degrees the motors would go in reverse until the encoders were equal to the degrees specified. This is the code with the auton correcting drive code that did not work.
Thank you

void autonTurnleft (int speed, float degrees)
{
SensorValue[rightDrive1] = 0;
SensorValue[leftDrive1] = 0;

while(rightDrive() < (degrees * 3) && leftDrive() < (degrees *  3))

{
	motor[backRightDrive] = -speed;
	motor[frontRightDrive] = -speed;
	motor[Drive3R] = -speed;
	motor[backLeftDrive] = speed;
	motor[frontLeftDrive] = speed;
	motor[Drive3L] = speed;
}

while (rightDrive() > (degrees * 3) && leftDrive() > (degrees *  3))

{
	motor[backRightDrive] = speed;
	motor[frontRightDrive] = speed;
	motor[Drive3R] = speed;
	motor[backLeftDrive] = -speed;
	motor[frontLeftDrive] = -speed;
	motor[Drive3L] = -speed;
}

while (rightDrive() == (degrees * 3) && leftDrive() == (degrees * 3))

{
	motor[backRightDrive] = 0;
	motor[frontRightDrive] = 0;
	motor[Drive3R] = 0;
	motor[backLeftDrive] = 0;
	motor[frontLeftDrive] = 0;
	motor[Drive3L] = 0;
}

}

It would probably be better to slow the motors before reaching the target position rather than putting them immeadiately into reverse. You can also send a small reverse value to the motors for a short time to help them stop the robot quickly. In pseudo code that may be something like this.


while( encoder_value < target_value  ) {
  // Full speed for 3/4 of the distance
  if( encoder_value < (target_value * 3 / 4) ) {
    run the motors full speed
  }
  else
  // slow for the final 1/4 of the distance
  if( encoder_value < target_value  ) {
    run the motors at slow speed
  }
}

send the motors a small reverse value (perhaps -10)
wait for 100mS
stop the motors