Ultrasonic Sensor turn in RobotC

We have a challenge in which the Ultrasonic sensor seems to be the most appropriate sensor for 30-seconds autonomous mode. As part of our challenge, we need to pick up objects and drop them off in a different location. I am trying to make the robot sense obstacles and turn:


#pragma config(Sensor, dgtl3,  sonarSensor,    sensorSONAR_cm)
#pragma config(Motor,  port1,           leftMotor,     tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port10,          rightMotor,    tmotorVex393, openLoop)


task main()
{
  int speed;            // Will hold a speed for the motors.
  int sonar_value;      // Will hold the current reading of the sonar sensor.
  int distance = 5;    // Specified distance to be at 5 centimeters.

  while(SensorValue[sonarSensor] > 10)  // While greater than 5 cm distance...
  {
    //...Move Forward
    motor[rightMotor] = 63;
    motor[leftMotor] = 63;
  }
	//...Turn
    if (SensorValue [sonarSensor]  < 8)
  {
  	motor [rightMotor] = -127;
  	motor [leftMotor] = 127;
  	wait1Msec (1000);
  }

    else
    {
      speed = (sonar_value - distance)*2;     // Set 'speed' equal to the 'sonar_value' minus desired 'distance'.
    }

  }

The robot stops, but it doesn’t turn, what am I missing?

Jpearman was spot on with his posting in this thread; the first while loop runs until the sonar sensor reads a value of 10, and then the code immediately checks to see if the sonar sensor value is less than 8. Since 10 is not less than 8, the if statement becomes false and the else condition is run instead.

To quote jpearman: