I’m trying to figure out how to program multiple buttons easily on the Vex Cortex. I want to be able to use a switch statement to figure out which button is pressed, instead of having to do eight if/then/else statements.
I found this page (http://www.robotc.net/wikiarchive/Using_switch_statements) on the RobotC wiki that is kind of what I am looking for, but I am trying to do it with more than two buttons, and I can’t think of how it would be done.
It could be done but you are probably better off with the nested if/then/else.
The issue with the switch statement is that it will only perform the test on one variable, that is, is looks at the value in the “switch( x )” part and then decides what to do. To test for multiple buttons you would have to combine them into a single variable to use in the switch statement, the example above could be extended but I’m not sure that the resultant code would really be any better.
You could do something like this.
var = (vexRT Btn5U ] << 3) + (vexRT Btn5D ] << 2) + (vexRT Btn6U ] << 1) + vexRT Btn6D ];
and then test var to be values
to detect the four button pressed states, but then you would need many other tests to handle multiple buttons.