I have two things I want to program in vex code pro v5 but I need to use some sort of code to replicate the pressed() function(which is buggy). I need to be able to press a button to deploy the wings and keep them down, and then when I press the same button again, they should retract. Right now, I’m using the pressing() function, but since it senses the button value too quickly, it just keeps going down and up extremely quickly and stuttering. Is there any way to program something like that?
Here is my code right now for making the wings go down and up using the same button:
How would I make a button press if it’s not in a while loop? The if(button press) function needs to be in the while loop in order for me to activate it. Otherwise, the button does nothing. But since it is in a while loop, the buttonPressing() function reads the button value too quickly, causing the button to be true and false at the same time which causes the piston to fire and retract at the same time, causing it to stutter.
The code given by jpearman should work so I would suggest using the Pressed callback example but don’t change any of the code. Just test it with the motor instead of the piston to make sure there isn’t something wrong with the way you are actuation the pistons.
If this works then I’m sure we can help you get the pistons working.
Just formatted your code to make it easier to read and fixed the unclosed function.
//Callback function run when ButtonL1 is first pressed
void toggleL1() {
//Shouldn't need the wait but uncomment it if the code isn't functioning as expected
//wait(100, msec);
piston.set(!piston.value());
}
//User Control Function
void usercontrol(void) {
//Assigning callback function
Controller1.ButtonL1.pressed(toggleL1);
while (1) {
wait(20, msec);
}
}
you’re right, BUT instead of putting Controller1.ButtonL1.pressed(toggle); in usercontrol, put it in main. Usercontrol tends to get run multiple times during a match, so putting it inside main would only make it run once.
Waaay back in the day we would ride in the backseat of our parents car (without a device) on long trips. Not knowing how long the trip would take, we would ask ‘Are we there yet?’ with the typical reply from the adults being ‘No!’. Then we would continue to drive them crazy by asking ‘Are we there yet?’ over and over until they finally said ‘We will let you know!’
In programming, your while loop is cycling through a list of code to evaluate. The pressing function is asking for the state of the controller button over and over.
int main() {
while (true) {
if (Controller1.ButtonR1.pressing()) { //Are we there yet?
Motor1.spin(forward); //Yes
} else {
Motor1.stop(); //No
}
wait(20, msec);
}
}
If we want the program to change the state of something like a motor (toggle on / off), it becomes a problem because the loop keeps the state flip flopping every 20 ms when the button is held down.
bool motorRunning = false;
int main() {
while (true) {
if (Controller1.ButtonR1.pressing()) { //Are you pressing the button?
//Yes Yes Yes Yes Yes...
motorRunning = !motorRunning; //True False True False True False ...
wait(20, msec);
}
}
To solve this problem the programming overlords created Events. Events are used outside of a loop and only fire once when the event is called. Instead of repeatedly asking ‘Are we there yet?’ we simply wait until the Event Handler ‘Lets us know’
bool wingIsOut = true;
//The 'callback' function that does the action.
void ChangeWingState(){
PneumaticA.set(wingIsOut); //Move the actuator
wingIsOut = !wingIsOut; //Flip the state (! means not or opposite)
}
int main() {
//Hey Event Handler, let us know when the button is pressed.
Controller1.ButtonA.pressed(ChangeWingState);
while (true) {
//Chassis code
wait(20,msec);
}
}
The other cool part is that it runs independent of the main loop. If you were to use a event on a limit switch, you could use it to reset the catapult. While the callback function is waiting for the arm to press the switch, it will not interrupt your drivetrain. Events allow separate actions to run at the same time without blocking each other.