@Tiezzz03: Yeah, the brain screen autonomous selector can be tricky, but it’s very useful once you get it figured out!
The exact code depends on how you want the selector to function. For example, you’d need to consider if you want two buttons to be able to both be selected at the same time. Or, instead, you might want a toggle that so that only one option can be selected at once (such as left side or right side).
Furthermore, you need to think about how user-friendly you want it to be. Personally, I’ve made mine so that I can toggle back and forth until I select “ready” (and I have a condition so that at least one button must be selected before “ready” is available to be selected). Once I select “ready”, whatever buttons I’ve selected, and the boolean flags that are assigned with each button, are locked in until the program is ended and started again.
A much simpler option would be to omit a “ready” button and just check for whatever button is pressed first, and go with that. Of course, if you select the wrong button, you would need to end the program and restart in order to change your selection, but that might not be a huge concern for you.
Here’s some general pseudocode of how I do it with the “ready” button. I’m sure that there are much shorter and more efficient methods of doing this, but this is the way I’ve found to be effective (@jpearman could most likely share some ways to improve this). Again, this code is pseudocode and will not work verbatim, but it should give you an idea of the concept.
Pseudocode
bool button1 = false, button2 = false, ready = false;
/* you'll also want to add constant integer values for the dimensions of each button
(in pixels). these will be used to draw the shape of the button as well as to define
the region of the brain screen which, when pressed, will change the boolean flag for
that button to true. I've found it to be easier to use an array for the coordinates
of each button (this pseudocode assumes you are using a rectangle for the button),
but you could easily just have individual constants for this */
int button1Coordinates[6] = {left x, right x, top y, bottom y, width, height};
int button2Coordinates[6] = {left x, right x, top y, bottom y, width, height};
int readyCoordinates[6] = {left x, right x, top y, bottom y, width, height};
/* now you would print what you want the initial brain screen to be prior to any selections. you can use Brain.Screen.setFont(),
Brain.Screen.setFillColor(), Brain.Screen.setPenColor(), Brain.Screen.setPenWidth(), etc. to customize your brain screen exactly the way
you want it to look. you can use the constants above (or reference the appropriate index, if you've used arrays) to draw the button */
Brain.Screen.drawRectangle(button1Coordinates[0], button1Coordinates[2], button1Coordinates[4], button1Coordinates[5]);
Brain.Screen.drawRectangle(button2Coordinates[0], button2Coordinates[2], button2Coordinates[4], button2Coordinates[5]);
Brain.Screen.drawRectangle(readyCoordinates[0], readyCoordinates[2], readyCoordinates[4], readyCoordinates[5]);
Brain.Screen.printAt(x coordinate where you want the text to start, y coordinate where you want the text to start, "button1 label");
Brain.Screen.printAt(x coordinate where you want the text to start, y coordinate where you want the text to start, "button2 label");
Brain.Screen.printAt(x coordinate where you want the text to start, y coordinate where you want the text to start, "ready");
while(ready == false) {
/* here's where button1 is toggled on. use the coordinates from above to check the region defined as button1 */
if(Brain.Screen.xPosition() > button1Coordinates[0] && Brain.Screen.xPosition() < button1Coordinates[1]
&& Brain.Screen.yPosition() > button1Coordinates[2] && Brain.Screen.yPosition() < button1Coordinates[3]
&& button1 == false) {
/* set the flags according to which button is pressed and if any other buttons need to be toggled off by button1 being pressed */
button1 = true;
button2 = false;
/* here you would clear the screen and print what you want to screen to look like now that button1 has been pressed. you would use similar
commands as above, specifically Brain.Screen.drawRectangle() and Brain.Screen.printAt() */
}
/* now, you'd do the same thing for button2 */
else if(Brain.Screen.xPosition() > button2Coordinates[0] && Brain.Screen.xPosition() < button2Coordinates[1]
&& Brain.Screen.yPosition() > button2Coordinates[2] && Brain.Screen.yPosition() < button2Coordinates[3]
&& button2 == false) {
/* set the flags according to which button is pressed and if any other buttons need to be toggled off by button2 being pressed */
button2 = true;
button1 = false;
/* again, here you would clear the screen and print what you want to screen to look like when that button2 has been pressed. */
}
/* and now for the ready button. I have an extra condition on this one. this code cannot run unless either button1 or button2 have been selected */
else if(Brain.Screen.xPosition() > readyCoordinates[0] && Brain.Screen.xPosition() < readyCoordinates[1]
&& Brain.Screen.yPosition() > readyCoordinates[2] && Brain.Screen.yPosition() < readyCoordinates[3]
&& (button1 == true || button2 == true)) {
ready = true; /* this flag will end the while loop as soon as the current iteration finishes */
/* again, here you would print what you would like see on the brain screen once ready is selected */
}
}
This code goes at the beginning of the pre_auton void function that is built into the competition template. Immediately after this while loop, you would put anything else that you need to run during the pre_auton period (for example, I reset the motor encoders, calibrate my inertial sensor, and check the color of the preload for automatic sorting during driver control).
Keep in mind that because of the while loop, no other code (i.e. autonomous or usercontrol) will execute until “ready” is selected, even if enabled with a competition switch or a field control system!
Hopefully this will help. Let me know if any of this is unclear, or if you have any more questions!