Auton Selection Code Help

Ok, so I am away from my team at the moment but am still tasked with getting some codes for them to test. I have been attempting to create an auton selector code and past versions have had trouble, so before they are able to test it as they are not with a brain right now I wanted to ask for help if anyone can see an obvious error in the code. (Note: I am sorry if the is another topic on this, I didn’t see one that seemed to fit directly to this to use.)

void pre_auton(void) {
vexcodeInit();
int loop = 1;
if(loop == 1){
while(loop == 1) {
Brain.Screen.setFillColor(color::blue);
Brain.Screen.drawRectangle(24,16,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(24,128,96,96);
Brain.Screen.setFillColor(color::cyan);
Brain.Screen.drawRectangle(136,16,96,96);
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::orange);
Brain.Screen.drawRectangle(248,16,96,96);
Brain.Screen.setFillColor(color::purple);
Brain.Screen.drawRectangle(248,128,96,96);
while(true){
if(Brain.Screen.pressing()){
int XPos = Brain.Screen.xPosition();
int YPos = Brain.Screen.yPosition();
if(XPos>24 && XPos<120 && YPos>16 && YPos<112){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (Launcher Spin Forward) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 1;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>24 && XPos<120 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (Laucher Spin in Reverse) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 2;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>136 && XPos<232 && YPos>16 && YPos<112){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (example Auton Name) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 3;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
  Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (example Auton Name) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 4;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>248 && XPos<344 && YPos>16 && YPos<112){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (example Auton Name) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 5;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (example Auton Name) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 6;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
wait(1,seconds);
}
}
}
wait(20,msec);
} else{
Brain.Screen.clearScreen();
wait(20,msec);
 loop = 1;
 }
}


//Auton
void autonomous(void) {
Brain.Screen.clearScreen();
Brain.Screen.drawImageFromFile("Logo.png",0,0);
if(Auton == 1) {
MotorLaunch.spin(forward);
wait(5,sec);
MotorLaunch.stop();
}
if(Auton == 2) {
MotorLaunch.spin(reverse);
wait(5,sec);
MotorLaunch.stop();
}
if(Auton == 3) {
Drivetrain.turnFor(-90,degrees);
Drivetrain.driveFor(forward, 500, mm);
}
if(Auton == 4) {
Drivetrain.driveFor(forward, -500, mm);
}
if(Auton == 5) {
Drivetrain.driveFor(forward, 150, mm);
Drivetrain.turnFor(23, degrees);
Drivetrain.turnFor(720, degrees);
}
if(Auton == 6) {
Drivetrain.driveFor(forward, 150, mm);
Drivetrain.turnFor(-23, degrees);
Drivetrain.turnFor(-720, degrees);
}
}

It is hard to see exactly the grouping with no tabs, however there is a crucial issue.

while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton = 5;
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
loop = 2;
}
}
}
if(XPos>248 && XPos<344 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("You Have Selected (example Auton Name) is this the code you would like to run?");
Brain.Screen.setFillColor(color::green);
Brain.Screen.drawRectangle(136,128,96,96);
Brain.Screen.setFillColor(color::red);
Brain.Screen.drawRectangle(248,128,96,96);
while(true) {
if(XPos>136 && XPos<232 && YPos>128 && YPos<224){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(3,4);
Brain.Screen.setFont(vex::fontType::mono60);
Brain.Screen.print("Comleted");
Auton =

That section has 2 while true loops, the second while true loop will never run because it will forever be in the top loop, you would need to conjoin them.

There are numerous threads on Auton Selectors.
https://www.vexforum.com/search?q=auton%20selector

One thing to note: When a programmer has multiple or nested while loops in their code, they are headed for a bad experience. You will want to incorporate if statements or separate tasks to handle your code.

You may want to look into Events. They can help you with organizing your code.
Below is a pseudo code concept using events to keep the code organized and clean.

void Auton1(){
  //Move robot red A
}

void Auton2(){
  //Move robot red B
}

void ScreenSelector(void){
  int x = Brain.Screen.xPosition();
  int y = Brain.Screen.yPosition();

  if(x > 0 && x < 120 && y > 0 && y < 120 )  Auton1();
  if(x > 120 && x < 240 && y > 0 && y < 120 ) Auton2();
}

int main() {
  Brain.Screen.pressed(ScreenSelector);

  while (true) {
    wait(20, msec);
  }
}
2 Likes