V5 Control mapping

Me and my team are first time competitors and we are trying to give buttons custom functions but I can’t seem to find how, please help.

If you’re not already, your code should start with the “competition template”. Select File > Open Examples and select “Competition Template” (not “Clawbot Competition Template”). This will create a new project with all the boilerplate code you need to respond to the field control system, operate during the autonomous period, etc.


Let’s say you want a motor to spin when button A is pressed. One way to do that would be to repeatedly check if the button is pressed within the infinite loop in usercontrol. This is known as “polling”:

void usercontrol(void){
    while (1){
        //pressing() will return true if the button is currently being pressed
        if (MyController.ButtonA.pressing()){
            //the button is currently pressed, so spin the motor
            MyMotor.spin(forward, 100, pct);
        }
        else {
            //the button is not currently pressed, so stop the motor
            MyMotor.stop();
        }
        
        wait(20, msec);
    }
}

Alternatively, another approach would be to tell the robot once what you want to happen when the button is pressed and released using a “callback”:

void usercontrol(void){
    //when the button is pressed, start spinning the motor:
    MyController.ButtonA.pressed([]{
        MyMotor.spin(forward, 100, pct);
    });

    //when the button is released, stop the motor:
    MyController.ButtonA.released([]{
        MyMotor.stop();
    });

    while (1){
        //no need for anything in this loop - we only need to register the callback once
        wait(20, msec);
    }
}

([]{} is a pretty weird syntax in c++, what it’s doing is defining an “anonymous function”, also called a “lambda”, to pass to pressed or released. You could also define two functions that do what you want to do when the button is pressed or released, and pass the names of those functions as callbacks, but that’s a lot of unnecessary extra syntax for this sort of thing. More on lambdas in c++ here.)

Whether it’s best to implement this sort of functionality using polling or callbacks depends on various factors, including what you want to happen when the button is pressed.

9 Likes

Are you using VEXcode text or VEXcode blocks

Thanks, I’ll try it out

We’re using Vex text

I tried it out but I am getting a clang error on line 8 with the “MyController” part

Well, do you have a controller configured with that name?

1 Like