V5 button lock

I need to know how to program a button lock in VEX C++. I got an answer before but i do not understand what it means. can someone explain it.

I had asked, “My team was wondering how to program a button lock on VCS. we want to be able to press a button to start the motor and press a button again to turn it off.” We included the image attached below.

This was the responce. “Right now you are using the .pressing() command which returns a Boolean for the button is being held down. If you want an action to perform after a single press of the button, use the .pressed() comment. Have it outside the while loop and have it call a method in which it will maybe change the state of a variable to 0 or 1. Then, if the variable is 0 do one action, but if it is 1, do the other.”

This is the current program.

#include “robot-config.h”
/---------------------------------------------------------------------------/
/* /
/
Description: Competition template for VCS VEX V5 /
/
/
/
---------------------------------------------------------------------------*/

//Creates a competition object that allows access to Competition methods.
vex::competition Competition;

/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/
/
/
You may want to perform some actions before the competition starts. /
/
Do them in the following function. You must return from this function /
/
or the autonomous and usercontrol tasks will not be started. This /
/
function is only called once after the cortex has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …

}

/---------------------------------------------------------------------------/
/* /
/
Autonomous Task /
/
/
/
This task is used to control your robot during the autonomous phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void autonomous( void ) { //this is the begining of the autonomous

Intake.setVelocity(100, vex::velocityUnits::pct);
CatapultLeft.setVelocity(100, vex::velocityUnits::pct);
CatapultRight.setVelocity(100, vex::velocityUnits::pct);

CatapultLeft.spin(directionType::fwd);
CatapultRight.spin(directionType::fwd);
vex::task::sleep(1000);
CatapultLeft.stop(brakeType::hold);
CatapultRight.stop(brakeType::hold);

CapFlip.stop(brakeType::hold);

//Define variables. You may need to change these variables for this program to work for your robot.
double wheelDiameterCM  = 10.16; //wheelDiameter is the measurement of a wheel from edge to edge in centimeters.
double travelTargetCM = 140; //travelTarget will define how far we want the robot to move in centimeter

//Calculate wheel circumferance: circumference = (wheelDiameter)(PI)
double circumference = wheelDiameterCM * M_PI; //Note that M_PI is from the math library which is included on line 14.    
//Now we know the robot will travel the Circumference per 360 degrees of rotation or (circumference / 360 degrees).
//We also know that are target distance is defined as travelTargetCM, but we do not know the degrees to of rotation. (traveTargetCM / ?)
//Using propotional reasoning we know (circumference / 360) = (travelTarget / degreesToRotate).
//Solving for our unkown (degreesToRotate) we get:
double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.

//Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
LeftBack.setVelocity(66, vex::velocityUnits::pct); 
RightFront.setVelocity(66, vex::velocityUnits::pct);
LeftFront.setVelocity(66, vex::velocityUnits::pct); 
RightBack.setVelocity(66, vex::velocityUnits::pct);


//Rotate the Left and Right Motor for degreesToRotate. 
LeftFront.rotateFor(-degreesToRotate, vex::rotationUnits::deg, false);
RightFront.rotateFor(-degreesToRotate, vex::rotationUnits::deg, false); 
LeftBack.rotateFor(-degreesToRotate, vex::rotationUnits::deg, false);
RightBack.rotateFor(-degreesToRotate, vex::rotationUnits::deg, false); 
//The motors will brake once they reach their destination.

vex::task::sleep(2000);

LeftFront.rotateFor(2543, vex::rotationUnits::deg, false);
RightFront.rotateFor(2543, vex::rotationUnits::deg, false); 
LeftBack.rotateFor(2543, vex::rotationUnits::deg, false);
RightBack.rotateFor(2543, vex::rotationUnits::deg, false); 
//The motors will brake once they reach their destination.

vex::task::sleep(2000);

                       
while(1) {
  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}    

}

/----------------------------------------------------------------------------/
/* /
/
User Control Task /
/
/
/
This task is used to control your robot during the user control phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
----------------------------------------------------------------------------*/

void usercontrol( void ) {
// User control code here, inside the loop
while (1){
int CatapultPCT = 100;
int IntakePCT = 100;
int CapFlipPCT = 80;

// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo 
// values based on feedback from the joysticks.
  
    LeftFront.spin(vex::directionType::fwd, Controller1.Axis3.value()/1.5, vex::velocityUnits::pct);
    RightFront.spin(vex::directionType::fwd, Controller1.Axis2.value()/1.5, vex::velocityUnits::pct);
    LeftBack.spin(vex::directionType::fwd, Controller1.Axis3.value()/1.5, vex::velocityUnits::pct);
    RightBack.spin(vex::directionType::fwd, Controller1.Axis2.value()/1.5, vex::velocityUnits::pct);

    if(Controller1.ButtonR1.pressing()){
        CatapultLeft.spin(directionType::fwd,CatapultPCT, vex::velocityUnits::pct);
        CatapultRight.spin(directionType::fwd,CatapultPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonR2.pressing()){
        CatapultLeft.spin(directionType::rev,CatapultPCT, vex::velocityUnits::pct);
        CatapultRight.spin(directionType::rev,CatapultPCT, vex::velocityUnits::pct);
    }
    else{
        CatapultLeft.stop(vex::brakeType::hold);
        CatapultRight.stop(vex::brakeType::hold);
    }
  
  
    if(Controller1.ButtonL1.pressing()){
        CapFlip.spin(directionType::fwd,CapFlipPCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonL2.pressing()){
        CapFlip.spin(directionType::rev,CapFlipPCT, vex::velocityUnits::pct);
    }
    else{
        CapFlip.stop(vex::brakeType::brake);
    }
  
    if(Controller1.ButtonUp.pressing()){
        Intake.spin(directionType::fwd,IntakePCT, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonDown.pressing()){
        Intake.spin(directionType::rev,IntakePCT, vex::velocityUnits::pct);
    }
    else{
        Intake.stop(vex::brakeType::brake);
    }
  
// ........................................................................
// Insert user code here. This is where you use the joystick values to 
// update your motors, etc.
// ........................................................................
 
  
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 

}
}

//
// Main will set up the competition functions and callbacks.
//
int main() {

//Run the pre-autonomous function. 
pre_auton();

//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );

//Prevent main from exiting with an infinite loop.                        
while(1) {
  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}    

}
button Qs.PNG

Ok, what you can do if you team wants motor to stay spinning without pressing is to define three buttons:

  • spin forward
  • spin reverse
  • stop

if(Controller1.ButtonUp.pressing()){
Intake.spin(directionType::fwd,IntakePCT, vex::velocityUnits::pct);
}
else if(Controller1.ButtonDown.pressing()){
Intake.spin(directionType::rev,IntakePCT, vex::velocityUnits::pct);
}
else if(Controller1.ButtonRight.pressing(){
Intake.stop(vex::brakeType::brake);
}

this would give three control buttons that will keep the motor doing what you tell them until told otherwise.