Example code to allow Alliance and Position Selection before autonomous

Im a mentor for a few teams and we’ve come up with some example code to allow saving a global variable for alliance and starting position before autonomous to avoid writing 2 or 4 different programs and trying to keep them all up to date.

Basically you just need to add these two functions and call launchAllianceAndPositionChooser() in your pre_auton function.

//Global variables to use throughout program 
//that can be changed by clicking the screen
bool ON_RED_ALLIANCE = true;
bool IN_LEFT_START_POSITION = true;

void allianceAndPositionSelect(){
  //Read the position of the screen that was clicked
  int x = Brain.Screen.xPosition();
  int y = Brain.Screen.yPosition();
  
  //Save which button was clicked in the global variable
  IN_LEFT_START_POSITION = x < 240;
  ON_RED_ALLIANCE = y < 130;

  //Position cursor and clear out last value 
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.clearLine();
  
  //Print out 
  if (ON_RED_ALLIANCE){
    Brain.Screen.print("Red Alliance - ");
  } else {
    Brain.Screen.print("Blue Alliance - ");
  }

  if (IN_LEFT_START_POSITION) {
    Brain.Screen.print("Left Position");
  } else {
    Brain.Screen.print("Right Position");
  }
}

void launchAllianceAndPositionChooser(){
  //Draw the Alliance and position buttons on the screen
  Brain.Screen.clearScreen();
  Brain.Screen.setFillColor(red);
  Brain.Screen.drawRectangle(10,30,225,95);
  Brain.Screen.drawRectangle(245,30,225,95);
  Brain.Screen.setFillColor(blue);
  Brain.Screen.drawRectangle(245,135,225,95);
  Brain.Screen.drawRectangle(10,135,225,95);

  //Listen to screen presses to save alliance and position in variables
  Brain.Screen.pressed(allianceAndPositionSelect);
}

I hope this can help some other teams as well

3 Likes

Elegant example!

James Pearman made a button matrix which we extended in Turning Point to select or de-select during autonomous. There were eight settings, meaning you could have lots of possibilities.