How do you code the LCD ? , i’m a new programmer that needs help on programming to you use the lcd to make a menu for different autonomous. Our team has 3 different autonomous, i would like to try to make a menu in the lcd to be able to change to other autonomous. Any help is appreciated.
For if you want to have the LCD used for an autonomous chooser, you might want to do a few things first. I have my code for our autonomous chooser in the pre autonomous task. That means that it will only run one time when the bot turns on, so you must turn the bot off and on again to choose a different mode.
I also make sure to have bDisplayCompetitionStatusOnLcd set to false, since I don’t want the competition include file using the LCD when I want to have an autonomous chooser.
It may be useful to make a global variable that will be used to keep track of your modes. A lot of people have this as an integer named
count
. Next you may also make constants for the buttons represented on the lcd
//Count initialization
int count;
//LCD Buttons
const short leftButton = 1;
const short middleButton = 2;
const short rightButton = 4;
In pre-autonomous, the first things I do are enable the LCD screen and clear it(although it really shouldn’t have anything on it)
//Enable backlight on LCD
bLCDBacklight = true;
//Clear lines on LCD
clearLCDLine(0);
clearLCDLine(1);
I then run a loop that loops while the center button isn’t pressed
while(nLCDButtons != centerButton){
}
You can then use a switch statement based on the count variable
switch(count){
case 0:
//Display first choice
displayLCDCenteredString(0, "Red Left");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
//This is to return to the last option in list
count = 4;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 1:
//Display second choice
displayLCDCenteredString(0, "Red Right");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 2:
//Display third choice
displayLCDCenteredString(0, "Blue Left");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 3:
//Display fourth choice
displayLCDCenteredString(0, "Blue Right");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
//Returns to first option in list
count++;
}
break;
case 4:
//Display fifth choice
displayLCDCenteredString(0, "None");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
//Returns to first option in list
count = 0;
}
break;
default:
//This will be run first that will set count to 0
count = 0;
break;
}
Of course, you would have to change your modes accordingly depending on the autonomous functions you run. All together, this is what your product may look like:
//Count initialization
int count;
//LCD Buttons
const short leftButton = 1;
const short middleButton = 2;
const short rightButton = 4;
void pre_auton()
{
// Set bStopTasksBetweenModes to false if you want to keep user created tasks
// running between Autonomous and Driver controlled modes. You will need to
// manage all user created tasks if set to false.
bStopTasksBetweenModes = true;
// Set bDisplayCompetitionStatusOnLcd to false if you don't want the LCD
// used by the competition include file, for example, you might want
// to display your team name on the LCD in this function.
bDisplayCompetitionStatusOnLcd = false; //We will use LCD for autonomous
//-------------------------AUTONOMOUS CHOOSER-------------------------------
//Enable backlight on LCD
bLCDBacklight = true;
//Clear lines on LCD
clearLCDLine(0);
clearLCDLine(1);
while(nLCDButtons != middleButton){
//Switch case that allows the user to choose from 4 different options
switch(count){
case 0:
//Display first choice
displayLCDCenteredString(0, "Red Left");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
//This is to return to the last option in list
count = 4;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 1:
//Display second choice
displayLCDCenteredString(0, "Red Right");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 2:
//Display third choice
displayLCDCenteredString(0, "Blue Left");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
count++;
}
break;
case 3:
//Display fourth choice
displayLCDCenteredString(0, "Blue Right");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
//Returns to first option in list
count++;
}
break;
case 4:
//Display fifth choice
displayLCDCenteredString(0, "None");
displayLCDCenteredString(1, "< Enter >");
waitForPress();
//Increment or decrement "count" based on button press
if(nLCDButtons == leftButton)
{
waitForRelease();
count--;
}
else if(nLCDButtons == rightButton)
{
waitForRelease();
//Returns to first option in list
count = 0;
}
break;
default:
//This will be run first that will set count to 0
count = 0;
break;
}
}
Now in your autonomous, all you have to do is an if statement on the
count
variable that corresponds to your autonomous mode.
Source,although I modified it a bit for my own code.
Hope this helps.
So your code has the problem a lot of people’s LCD autonomous selector have. What happens if the cortex restarts or you don’t press the LCD. You can’t move at all. Take a look over here
You’re correct. I make sure that I do press the button on the cortex, but if something were to happen then it would cause the bot to not even go into usercontrol. This could potentially be fixed by also implementing bIfIRobotDisabled within the conditional for the while loop, but I was short on time so I did not implement it at the time, but rather ensured I pressed the button on the LCD. This could also have a tremendous effect if the battery causes the cortex to restart, like you mentioned. This is only a temporary solution that at least shows how it can be used to select an autonomous routine.
Ya the easiest thing to do is just run it in a seperate task.