LCD code chooser Example

So over the years a lot of people have had issues with making LCD autonomous selectors. While the premise is quite simple everyone started with the “code chooser” sample program built into ROBOTC. The code itself is fine but as it is not built on the competition template people constantly struggle with applying it to what is a effectively a multithreaded system. Peoples implementations of the code chooser code commonly will not allow the robot to go into driver control or autonomous unless a new autonomous is chosen every time the robot is restarted. This happens because pre_auton is blocking until something is selected and mid match that isn’t always an option. In light of this I made a new example program that is simple and built off of the competition template so it doesn’t have any of these problems.

TLDR If your making LCD autonomous selection code start with this. Its only 50 lines more than the competition template and easy to read.

It can always be found here

Here is a raw text version

//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*        Description: Competition template for VEX EDR                      */
/*                                                                           */
/*---------------------------------------------------------------------------*/

// This code is for the VEX cortex platform
#pragma platform(VEX2)
#pragma competitionControl(Competition)

//Main competition background code...do not modify!
#include "Vex_Competition_Includes.c"
int auton = 0;


//ADD all autonomous choices
void displayAuton(){
	switch(auton){
	case 1:
		displayLCDCenteredString(0, "red auton");
		break;
	case -1:
		displayLCDCenteredString(0, "blue auton");
		break;
	case 0:
		displayLCDCenteredString(0, "ram auton");
		break;
	default:
		displayLCDCenteredString(0, "No auton");
		break;
	}
}


//DO NOT MODIFY
task LCDControl()
{
	clearLCDLine(0);
	clearLCDLine(1);
	bool noButtonsPressed = true;
	displayAuton();
	while(true){
		if(noButtonsPressed){ // only update auton if a button is pressed AND wasn't pressed previously
			switch(nLCDButtons){
			case kButtonLeft:
				auton--;
				displayAuton();
				break;
			case kButtonCenter:
				stopTask(LCDControl);
				break;
			case kButtonRight:
				auton++;
				displayAuton();
				break;
			}
		}//if
		noButtonsPressed = !nLCDButtons; //update if there is a button currently pressed
		wait1Msec(20);
	}//while
}//task


void pre_auton()
{
	bStopTasksBetweenModes = true;
	bDisplayCompetitionStatusOnLcd = false;
	startTask(LCDControl);
}

task autonomous()
{
	stopTask(LCDControl);
	switch(auton){
	case 1:
		//redauton();
		break;
	case 0:
		//ramauton()
		break;
	case -1:
		//blueauton();
		break;
	default:
		//Don't run auton
		break;
	}
}

task usercontrol()
{
	// User control code here, inside the loop
	stopTask(LCDControl);
	while (true)
	{
		motor[port1]= 50;
		wait1Msec(50);
	}
}

If there is interest I might make more versions. Call this version “beginner” and then implement more useful features in an intermediate or expert version. The goal of this version is to be as simple for people to use as possible and to replace the “code chooser” sample program in competition programs.

This is awesome! Thank you for creating this program.

Hi Tabor, thank you for providing the code. We are currently using the example from RobotC but yours looked simpler so tried it out. Could you tell me what we are doing wrong as we couldn’t get it to work. When we used the competition switch in RobotC, we pressed Start then Disable (this is what we do with the RobotC example) but it doesn’t allow us to choose an “auton” on the LCD and you see the word “disabled” with the time ticking off. We can follow the logic behind your code but don’t seem to be able to select an “auton” on the LCD. We tried just pressing Start and got the same result. Are we missing a line of code? Thanks for your help.

Okay so you should download.
Press disable
Press start

This is the order you would experience at a competition because the robot is turned on while plugged into the field control. If at any point you enter autonomous or driver control the task is killed because it isn’t necessary. This means is you run without a competition controller the robot will go right into driver control and kill the LCD choosing system.