Problem With Coding Shaft Encoders

Hello,

Does anyone see a problem with my code below? I’m trying to get the robot to go straight, stop for 2 seconds, make a turn, stop for 2 seconds, and then go straight again. When I compile and download the code, the robot goes straight, does not take the 2 second pause, turns, and then stops without executing the last set of commands to go straight. Also, since each encoder uses 2 ports on the Cortex, does it matter what order the plugs go in? Is there a plug 1 and plug 2 for each encoder? Any help would be greatly appreciated.

Thanks!
#pragma config(Sensor, dgtl1, leftEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl3, rightEncoder, sensorQuadEncoder)
#pragma config(Motor, port1, leftMotor, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port10, rightMotor, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

/Reset encoders to 0/
SensorValue [leftEncoder]=0;
SensorValue [rightEncoder]=0;

while (SensorValue [rightEncoder]<200)
{
motor[leftMotor]=50;
motor[rightMotor]=50;
}
/take a 2 second pause before the next command/
wait1Msec(2000);

/*Reset encoders to 0*/

SensorValue [leftEncoder]=0;
SensorValue [rightEncoder]=0;

/Make left point turn/
while (SensorValue [rightEncoder]<170)
{
motor[leftMotor]=-50;
motor[rightMotor]=50;
}

/take a 2 second pause before the next command/
wait1Msec(2000);

/Reset encoders to 0/
SensorValue [leftEncoder]=0;
SensorValue [rightEncoder]=0;

while (SensorValue [rightEncoder]<300)
{
motor[leftMotor]=50;
motor[rightMotor]=50;
}
}

Let’s look at the code, it starts with

  /*Reset encoders to 0*/
  SensorValue [leftEncoder]=0;
  SensorValue [rightEncoder]=0;

  while (SensorValue [rightEncoder]<200)
    {
    motor[leftMotor]=50;
    motor[rightMotor]=50;
    }
  /*take a 2 second pause before the next command*/
  wait1Msec(2000);

You reset encoders, start motors (as presumably encoders are still at 0), wait until the encoder count reaches 200 counts, then have a 2 second delay. When the while loop finishes the motors will still be running, you need to stop them if the robot should stop before turning.

The same thing happens with the turn and then move forwards, there is no motor[leftMotor] = 0; etc.

The order in which you plugin the encoder cables will determine which direction they count, you should try and make sure that the encoder count increment when the motor is given a positive control value, you can see the encoder count in the sensors debug window.

Something like this.

#pragma config(Sensor, dgtl1, leftEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl3, rightEncoder, sensorQuadEncoder)
#pragma config(Motor, port1, leftMotor, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port10, rightMotor, tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{

  /*Reset encoders to 0*/
  SensorValue [leftEncoder]=0;
  SensorValue [rightEncoder]=0;

  while (SensorValue [rightEncoder]<200)
    {
    motor[leftMotor]=50;
    motor[rightMotor]=50;
    }
  motor[leftMotor]=0;
  motor[rightMotor]=0;
  /*take a 2 second pause before the next command*/
  wait1Msec(2000);

  /*Reset encoders to 0*/
  SensorValue [leftEncoder]=0;
  SensorValue [rightEncoder]=0;

  /*Make left point turn*/
  while (SensorValue [rightEncoder]<170)
  {
  motor[leftMotor]=-50;
  motor[rightMotor]=50;
  }

  motor[leftMotor]=0;
  motor[rightMotor]=0;
  /*take a 2 second pause before the next command*/
  wait1Msec(2000);

  /*Reset encoders to 0*/
  SensorValue [leftEncoder]=0;
  SensorValue [rightEncoder]=0;

  while (SensorValue [rightEncoder]<300)
  {
  motor[leftMotor]=50;
  motor[rightMotor]=50;
  }
  
  motor[leftMotor]=0;
  motor[rightMotor]=0;
}