Hello everyone, I have a simple question about my autonomous selector. Why does the “lcdButton” part turn red?
lcdButton is not a defined class. I am not sure where you got the idea of lcdButton from but it may be a legacy class that no longer exists or it may have never existed in the first place. In order to make a auton selector you need to use the draw functions and the xPosition and yPosition functions to get the positions of the last brain touch. You can look at the vexcode v5 brain screen api to see what functions are available. You can also look at this guide from my organization: V5 GUI for Auton Selector | Rolling Robots
So I have made it this far now. I have written code to draw the over under field on the V5 brain in the pre-autonomous area. But based off of the links that you have sent me, I can’t comprehend what to do next. The end goal is for me to press one of the buttons on the V5 brain, either “RED 1,” “RED 2,” “BLUE 1,” or “BLUE 2” and execute code for autonomous. I don’t know how to code the next steps. Thank you for all the help.
You can use the vex::brain::lcd::pressed()
or vex::brain::lcd::pressing()
functions to detect press interactions on the screen.
pressed(void(*callback)(void))
registers a callback function that should be executed every time the screen is pressed.pressing()
returns a boolean value that represents whether or not the screen is being pressed at the time the function is called.
Here is an event-based template:
void screenPressedHandler()
{
int xPosition = Brain.Screen.xPosition(); // Where the screen was pressed on the x-axis
int yPosition = Brain.Screen.yPosition(); // Where the screen was pressed on the y-axis
if (
(xPosition >= xMinimumBoundary && xPosition <= xMaximumBoundary)
&&
(yPosition >= yMinimumBoundary && yPosition <= yMaximumBoundary)
)
{
// Button press logic here
}
}
int main() {
Brain.Screen.pressed(screenPressedHandler);
}
xMinimumBoundary
, xMaximumBoundary
, yMinimumBoundary
, and yMaximumBoundary
are all integers that can be used to represent a rectangular space on a plane, which in this case is the screen.
The code can be simplified, written differently, and support more than one button through the use of else if
statements, but I’ll leave that implementation to you because of rule <G4>.
When I finished the program for my autonomous selector, I got an error highlighting the curly braces saying “Function Definition Isn’t Allowed Here.” What does this mean?
In C++, you cannot define functions within other functions.
I do not know where to define what happens when I press a button on my V5 brain.
Does anyone have example code for autonomous selectors that I could analyze?
Here is some relatively simple example code. Please try and take the time to understand what is going on and I am confident you will be able to find a way to adapt this to make your current code work
why your previous code doesn't work
The code is almost there but not quite. The xPosition and yPosition functions get the last spot the brain was pressed but where you have them they will never get updated or called by vex because they are not inside of a function. Using a function callback to detect when the brain is pressed and call a function (that updates your x and p positions) will update your values correctly.
Thank you very much for giving me advice!