How do I use the controller button pressed command

So I was looking at the Controller.Button.pressed(); command and it gives me and error saying their is no callback so I looked up on the vex reference command that shows all the command to see what a call back was and I still don’t understand. If someone can explain how to use this function and how to make a call back that’d be great.


Controller.Button.pressed()

takes a function as a parameter and everytime that button is pressed it calls that function

I’m sorry but I don’t know what that means in terms of my code to help me use the function.

P.S. I’m really bad at coding

Let’s say if every time you pressed button A you wanted to print how many times button A has been pressed you could do something like this:


#include "robot-config.h"
vex::controller Controller;

int timesPressed = 0;

void function(){
    timesPressed++;//add one to timesPressed
    Controller.Screen.clearScreen();
    Controller.Screen.print("Pressed A Button %i Times", timesPressed);//prints how many times Button A was pressed to the controller's screen
}

int main() {
    Controller.ButtonA.pressed(function);//every time Button A is pressed function() is run
    while(true){//so program doesn't exit
        vex::task::sleep(25);
    }
}

instead of this:


#include "robot-config.h"
vex::controller Controller;

int timesPressed = 0;

void function(){
    timesPressed++;//add one to timesPressed
    Controller.Screen.clearScreen();
    Controller.Screen.print("Pressed A Button %i Times", timesPressed);//prints how many times Button A was pressed to the controller's screen
}

int main() {
    while(true){
        if (Controller.ButtonA.pressing()){
            function();
            vex::task::sleep(250);//so function() isn't run a lot of time for one button press
        }
        vex::task::sleep(25);
    }
}

Does that help?

Yes this helps a lot! I think I understand now. thank you

So since you have it outside the while loop wouldn’t I not be able to really use it in UserControl because it is outside the while loop? Am I able to put it inside the while loop?

What it does is set the function to call, it doesn’t check to see if the button is pressed

So after setting the function, anytime the button is pressed the function is called

So it doesn’t have to be in the while loop and it will still work

ok thank you for the help!

no problem

I find this odd considering what VEX’s command reference’s example of its usage is. Here is a snippet:

if(Controller1.ButtonA.pressing()) { //If the A button is pressed...
    //...Spin the claw motor forward.
    ClawMotor.spin(directionType::fwd, clawSpeedPCT, velocityUnits::pct);
}

I was talking about


Controller.ButtonA.pressed();

yea, this is used when polling the button, it returns a boolean indicating if the button is currently pressed or not.


Controller1.ButtonA.pressing()

These are used when you want your code to be event based.


Controller.ButtonA.pressed( callback_pressed );
Controller.ButtonA.released( callback_released );

A callback used an an event is running in its own thread, that means you can block using time consuming code or a while loop if you really want to, for example, this code

using namespace vex;

controller p = controller( controllerType::primary );

void b1() {
    Brain.Screen.setPenColor( vex::color::green );

    while( p.ButtonL1.pressing() ) {
      Brain.Screen.printAt( 200, 180, "L1 pressed");
      vex::this_thread::sleep_for(100);
    }
    Brain.Screen.printAt( 200, 180, "             ");
    return;
}

int main() {
  int count = 0;

  p.ButtonL1.pressed( b1 );
  
  while(1) {
    Brain.Screen.printAt( 10, 50, "Hello %d", count++ );
    // Allow other tasks to run
    this_thread::sleep_for(10);
  }
}

Will call the function “b1” when the L1 button is pressed and stay in that function until it is released.

Another interesting thing about that code, notice I set pen color to green in the b1 function here.


Brain.Screen.setPenColor( vex::color::green );

and yet the “Hello” text in the main thread keeps printing in white.
This is because for many screen properties (not all), each thread has its own graphics context, that is, the font, pen color, fill color and one or two other things are unique to the thread they are set in and not affected by changes in others.

Thanks. I don’t know why I’d read “pressing” and “pressed” the same. Probably something to do with two little children scampering about. I do like event-based controls and use them all the time in Java.

That’s nice to know. Thanks.

They have unfortunately similar names.