How can I tell the code to re-run?

Ok, so I have been working on an auton selector for our code. For a portion of this I plan to have a cancel button meant to send me back to the original code that runs. Currently I have my code set up like this

int loop = 1;
if(loop == 1){ 

if(XPos>248 && XPos<344 && YPos>128 && YPos<224){

loop = 2;
}
} else{

Brain.Screen.clearScreen();

wait(20,msec);

loop = 1;

} 

This should work by only running if loop = 1, then setting loop to 2, after this because it doesn’t = 1 it is set to 1 which should restart the loop. Please give me any feedback you can.

It isn’t looping back to the start, because you have not actually used a loop. There is nothing in your code to tell it to go back to the top after setting loop to 1 or 2.

while(loop == 1){} will make the code go to the top of the loop if, at the end of the loop, loop == 1. Depending on what you want to do, you should either replace the first if statement with a while loop or encase all the code (except int loop = 1;) in the while loop.

Also, check your curly braces - either I’m missing something, or they don’t add up. There are two closing braces before else { and I can’t see why.

Side note: for some reason, all posts have to be approved before they appear. Sorry if this turns out to be the fifth identical response to your problem, but I can’t actually see if anyone else has replied and what they have typed.

You could try a while loop. You can make a bool variable and have while (variable) { }. When it is true it will run and when you set the variable to false it will not.

- Henry 344E

is this inside a loop? if it is not, it would check once, and then stop.

1 Like

You could use an event to select the Auton. Instead of looping, it would fire once whenever you touched the screen. A global variable autonMode would hold the last selected value.

Below is concept code - untested. There are also several posts on this forum related to an Auton Selector if you want to review them.

int autonMode = 0;

void DrawAutonButtons()
{
  Brain.Screen.clearScreen();
  Brain.Screen.setPenColor(yellow);
  Brain.Screen.setFillColor(blue); 
  Brain.Screen.drawRectangle(56,128,96,96); //Blue Button
  Brain.Screen.setFillColor(red); 
  Brain.Screen.drawRectangle(152,128,96,96); //Red Button
  Brain.Screen.setFillColor(green); 
  Brain.Screen.drawRectangle(248,128,96,96); //Green Button
}

void ScreenPressed(){
  int x = Brain.Screen.xPosition(); 
  int y = Brain.Screen.yPosition(); 
  if(x > 56  && x < 152 && y > 128 && y < 224 ) autonMode = 0;
  if(x > 152 && x < 248 && y > 128 && y < 224 ) autonMode = 1;
  if(x > 248 && x < 344 && y > 128 && y < 224 ) autonMode = 2;
}

int autonControl(){
  if(autonMode == 0 ) {
    //Run Blue A
  }
  if(autonMode ==1 ) {
    //Run Red A
  }
}

int main() {
  DrawAutonButtons();
  Brain.Screen.pressed(ScreenPressed);
  while(true){
    wait(20, msec);
  }
}
1 Like

Thanks for the help, I completely didn’t notice that. In truth I was trying to tell whether this code would restart at the beginning but I had yet to be able to test so this answered my question thank you.