Didn’t get around to posting this yesterday, ROBOTC crashed on me and I lost the changes. Anyway, here is a modified version of the LCD user interface that will quit if the robot becomes enabled. If there is no competition switch connected then this won’t run at all, then again, you have no way to enter autonomous without the switch so it doesn’t really matter.
//Declare count variable to keep track of our choice
int count = 0;
//Wait for Press--------------------------------------------------
void waitForPress()
{
while( (nLCDButtons == 0) && bIfiRobotDisabled )
wait1Msec(5);
}
//----------------------------------------------------------------
//Wait for Release------------------------------------------------
void waitForRelease()
{
while( (nLCDButtons != 0) && bIfiRobotDisabled )
wait1Msec(5);
}
void pre_auton()
{
bStopTasksBetweenModes = true;
//Completely clear out any previous sensor readings by setting the port to "sensorNone"
SensorType[in2] = sensorNone;
wait1Msec(1000);
//Reconfigure Analog Port 2 as a Gyro sensor and allow time for ROBOTC to calibrate it
SensorType[in2] = sensorGyro;
wait1Msec(2000);
// Turn on LCD Backlight
bLCDBacklight = true;
//------------- Beginning of User Interface Code ---------------
//Clear LCD
clearLCDLine(0);
clearLCDLine(1);
//Loop while center button is not pressed
while( (nLCDButtons != kButtonCenter) && bIfiRobotDisabled )
{
//Switch case that allows the user to choose from 4 different options
switch(count)
{
case 0:
//Display first choice
displayLCDCenteredString(0, "Right Middle");
displayLCDCenteredString(1, "< Enter >");
break;
case 1:
//Display second choice
displayLCDCenteredString(0, "Left Middle");
displayLCDCenteredString(1, "< Enter >");
break;
case 2:
//Display third choice
displayLCDCenteredString(0, "No Autonomous");
displayLCDCenteredString(1, "< Enter >");
break;
case 3:
//Display fourth choice
displayLCDCenteredString(0, "Prog. Skills");
displayLCDCenteredString(1, "< Enter >");
break;
case 4:
//Display fifth choice
displayLCDCenteredString(0, "Right Hang");
displayLCDCenteredString(1, "< Enter >");
break;
case 5:
//Display sixth choice
displayLCDCenteredString(0, "Left Hang");
displayLCDCenteredString(1, "< Enter >");
break;
default:
count = 0;
break;
}
// wait for a button press
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == kButtonLeft)
{
waitForRelease();
if(--count < 0)
count = 5;
}
else
if(nLCDButtons == kButtonRight)
{
waitForRelease();
if(++count > 5)
count = 0;
}
}
}
I made several changes.
-
You don’t need to define the buttons (const short leftButton = 1; etc.) as ROBOTC has these pre-defined for you.
-
I added a test for the robot being enabled in waitForPress, waitForRelease and the while loop in the pre_auton code, this will cause these functions to quit if the robot is enabled.
-
The wait1Msec(5); in waitForPress and waitForRelease was outside the braces and therefore meaningless, I revised that code so the cpu gets some time.
-
The check for the left and right buttons in every branch of the switch statement was sort of redundant so I simplified that.
-
The “displayLCDCenteredString(1, “< Enter >”);” statements were not really working as they were longer than the 16 characters the LCD can display, I reduced to 16 chars and removed the tabs which caused the weird display (unless did you want that ? )
-
I made the left and right buttons wrap around the selection at both ends. If you add more choices then you will need to revise the code that wraps from choice 5 to 0.
I also simplified (but did not change) your driver control code, too many nested while loops.
task usercontrol()
{
string mainBattery, backupBattery;
// User control code here, inside the loop
while (true)
{
//Remote Control Commands
motor[frontLeft] = vexRT[Ch2] - vexRT[Ch1];
motor[backLeft] = vexRT[Ch2] + vexRT[Ch1];
motor[frontRight] = vexRT[Ch3] + vexRT[Ch4];
motor[backRight] = vexRT[Ch3] - vexRT[Ch4];
//Lift
if(vexRT[Btn5U] == 1)
{
motor[liftHighLeft] = 127;
motor[liftHighRight] = 127;
motor[liftLowLeft] = 127;
motor[liftLowRight] = 127;
}
else
if(vexRT[Btn5D] == 1)
{
motor[liftHighLeft] = -127;
motor[liftHighRight] = -127;
motor[liftLowLeft] = -127;
motor[liftLowRight] = -127;
}
else
{
motor[liftHighLeft] = 10;
motor[liftHighRight] = 10;
motor[liftLowLeft] = 10;
motor[liftLowRight] = 10;
}
//Intake Control
if(vexRT[Btn6U] == 1)
{
motor[intakeRight] = 127;
motor[intakeLeft] = 127;
}
else
if(vexRT[Btn6D] == 1)
{
motor[intakeRight] = -127;
motor[intakeLeft] = -127;
}
else
{
motor[intakeLeft] = 0;
motor[intakeRight] = 0;
}
clearLCDLine(0); // Clear line 1 (0) of the LCD
clearLCDLine(1); // Clear line 2 (1) of the LCD
//Display the Primary Robot battery voltage
displayLCDString(0, 0, "Primary: ");
sprintf(mainBattery, "%1.2f%c", nImmediateBatteryLevel/1000.0,'V'); //Build the value to be displayed
displayNextLCDString(mainBattery);
//Display the Backup battery voltage
displayLCDString(1, 0, "Backup: ");
sprintf(backupBattery, "%1.2f%c", BackupBatteryLevel/1000.0, 'V'); //Build the value to be displayed
displayNextLCDString(backupBattery);
//Short delay for the LCD refresh rate
wait1Msec(25);
}
}
Also, reduced the loop delay, 100mS was a bit long.