Ok, try this. You can ignore all the other changes I made (and did not test), I know you didn’t ask me to change anything else but I wanted to show you some of the types of things you could do to make the code more modular. Find the sonar stuff near the end, again, I cannot actually try this today so it may be wrong but it should give you the idea. I will send some more comments privately.
#pragma config(Sensor, in1, arm, sensorPotentiometer)
#pragma config(Sensor, dgtl1, sonarRight, sensorSONAR_inch)
#pragma config(Sensor, dgtl5, encoderRight, sensorQuadEncoder)
#pragma config(Sensor, dgtl8, sonarLeft, sensorSONAR_inch)
#pragma config(Sensor, dgtl10, encoderLeft, sensorQuadEncoder)
#pragma config(Motor, port2, frontRight, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, backRight, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port4, liftRight, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port5, intakeRight, tmotorServoContinuousRotation, openLoop, reversed)
#pragma config(Motor, port6, frontLeft, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port7, backLeft, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port8, liftLeft, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port9, intakeLeft, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
// This is the sonar distance from the trough we want to attain
#define TROUGH_DISTANCE 6
// A timeout in case the encoders fail - set larger than you expect
// driving to take, currently 7 seconds
#define DRIVE_TIMEOUT 7000
// Function to set drive motor speed
void
SetDriveMotors( int leftSpeed, int rightSpeed )
{
motor[frontLeft] = leftSpeed;
motor[backLeft] = leftSpeed;
motor[frontRight] = rightSpeed;
motor[backRight] = rightSpeed;
}
// A function to drive using the left encoder to measure distance
//
void
DriveUsingLeftEncoder( int distance, int leftSpeed, int rightSpeed )
{
//Clear Encoders
SensorValue[encoderLeft] = 0;
SensorValue[encoderRight] = 0;
// Use T1 as timeout
time1[T1] = 0;
if( distance > 0 )
{
while((SensorValue[encoderLeft] < distance) && (time1[T1] < DRIVE_TIMEOUT))
SetDriveMotors( leftSpeed, rightSpeed );
}
else
{
while((SensorValue[encoderLeft] >= distance) && (time1[T1] < DRIVE_TIMEOUT))
SetDriveMotors( leftSpeed, rightSpeed );
}
}
// A function to drive using the right encoder to measure distance
//
void
DriveUsingRightEncoder( int distance, int leftSpeed, int rightSpeed )
{
//Clear Encoders
SensorValue[encoderLeft] = 0;
SensorValue[encoderRight] = 0;
// Use T1 as timeout
time1[T1] = 0;
if( distance > 0 )
{
while((SensorValue[encoderRight] < distance) && (time1[T1] < DRIVE_TIMEOUT))
SetDriveMotors( leftSpeed, rightSpeed );
}
else
{
while((SensorValue[encoderRight] >= distance) && (time1[T1] < DRIVE_TIMEOUT))
SetDriveMotors( leftSpeed, rightSpeed );
}
}
task main()
{
//...Move forward.
DriveUsingLeftEncoder( 300, 63, 63 );
wait1Msec(250);
//...90 degree right turn.
DriveUsingLeftEncoder( 300, 63, -63 );
wait1Msec(250);
//...Move forward.
DriveUsingLeftEncoder( 200, 63, 63 );
wait1Msec(250);
//...90 dgree left turn.
DriveUsingRightEncoder( 250, -63, 63 );
wait1Msec(250);
// Start Intake - seems like we needed left as well
motor[intakeLeft] = 127;
motor[intakeRight] = 127;
//...Move forward.
DriveUsingLeftEncoder( 750, 40, 40 );
wait1Msec(1000);
//...Back away from sack stack.
DriveUsingRightEncoder( -25, -63, -63 );
wait1Msec(750);
// Stop intake - seems like we needed left as well
motor[intakeLeft] = 0;
motor[intakeRight] = 0;
wait1Msec(250);
//...90 degree right turn.
DriveUsingLeftEncoder( 275, 63, -63 );
//...Move towards left side of field.
DriveUsingRightEncoder( -575, -63, -63 );
wait1Msec(250);
//...90 degree right turn.
DriveUsingLeftEncoder( 255, 63, -63 );
// Stop
SetDriveMotors( 0, 0 );
// Take a breath
wait1Msec(1000);
// Ok - so here is the sonar code
// Drive backards until both sonars are TROUGH_DISTANCE inches from the trough
// Stop left or right independently
// Start driving backwards - don't know what speed
SetDriveMotors( -50, -50 );
// do this until both are within distance, may need to slow down
while( (SensorValue[sonarLeft] > TROUGH_DISTANCE) || (SensorValue[sonarRight] > TROUGH_DISTANCE) )
{
// If the left one triggers
if( SensorValue[sonarLeft] <= TROUGH_DISTANCE )
{
// Stop left
motor[frontLeft] = 0;
motor[backLeft] = 0;
}
// If the right one triggers
if( SensorValue[sonarRight] <= TROUGH_DISTANCE )
{
// Stop right
motor[frontRight] = 0;
motor[backRight] = 0;
}
// Don't hog cpu
wait1Msec(10);
}
// Make sure all motors are stopped
SetDriveMotors( 0, 0 );
// Off into driver control
//Loop Forever
while(1 == 1)
{
//Remote Control Commands
// Ok so I added a deadband for you as well
if( (abs(vexRT[Ch1]) > 10) || (abs(vexRT[Ch2]) > 10) || (abs(vexRT[Ch3]) > 10) || (abs(vexRT[Ch4]) > 10))
{
motor[frontRight] = vexRT[Ch2] - vexRT[Ch1];
motor[backRight] = vexRT[Ch2] + vexRT[Ch1];
motor[frontLeft] = vexRT[Ch3] - vexRT[Ch4];
motor[backLeft] = vexRT[Ch3] + vexRT[Ch4];
}
else
{
motor[frontRight] = 0;
motor[backRight] = 0;
motor[frontLeft] = 0;
motor[backLeft] = 0;
}
//Arm Control
if(vexRT[Btn5U] == 1)
{
motor[liftLeft] = 127;
motor[liftRight] = 127;
}
else if(vexRT[Btn5D] == 1)
{
motor[liftLeft] = -63;
motor[liftRight] = -63;
}
else
{
motor[liftLeft] = 0;
motor[liftRight] = 0;
}
//Intake Control
if(vexRT[Btn6U] == 1)
{
motor[intakeLeft] = 127;
motor[intakeRight] = 127;
}
else if(vexRT[Btn6D] == 1)
{
motor[intakeLeft] = -127;
motor[intakeRight] = -127;
}
else
{
motor[intakeLeft] = 0;
motor[intakeRight] = 0;
}
}
}