How To Program A Button Sensor

I am having issues with my bumper sensor program. When the bumper is pressed once, I want it to rotate a motor. The program keeps calling for a “callback” to be specified but I have no clue what this means. Anyone have any suggestions?

BuperA.pressed() sets up a callback. A callback is a function that runs if an event happens. In order to use a callback, you need to place the code that you want to run into a function like this

void raiseLift(){
    Lift.rotateFor(vex::directionType::fwd, 50, rotationUnits::deg);
}

then to create a callback to the function, use the following in main()

BumperA.pressed(raiseLift);

Yes, I had this exact same problem. what you need to do is create a subroutine (void), that specifies the task you want, and then have the button.pressed() call that subroutine.

void (lifterfunction){
Lift.rotateFor(vex::directionType::fwd, 50 , rotationUnits::deg);
}
BumperA.pressed(lifterfunction);

(note, I may have some of the exact syntax wrong for the subroutine, but I’m sure you know how to do that). Also note: The Button.pressed() command is not a loop, it is its own command. So don’t use “if”. It will run as if it was a conditional loop but you have to set it up like I showed.

Use

if (BumperA.pressing()){
  //code you want to execute here
}

try using this instead:

if(BumperA.pressing())
{
    //Code to run when pressed
}

.pressed() is completely different. It doesn’t return a boolean value, rather it sets up a callback which can be thought of as a function that is called whenever the button is pressed. Here is a use case for .pressed():

void MyCallbackFunction()
{
    Brain.Screen.print("Button was pressed!");
}

void Usercontroll()
{
    // outside of main loop:
    // note that the callback is not getting called, by not calling it
    // we are getting the address of this function so that vex's callback
    // system can use it.
    Controller1.ButtonL1.pressed( MyCallbackFunction );
    
    while(true)
    {
        //main loop
    }
    //Don't put .pressed in a loop!
}

You are doing the if statement incorrectly. You need to put brackets instead of a semicolon. Like this ‘’’if(bumperA.pressed()){
Lift.rotateFor(fwd,50,degrees);
}’’’

It wants you to have it call a function whenever you run button.pressed(), but if you run button.pressing() then it returns true if the button is pressing and false if the button isn’t pressed

So I set up when the button is pressed once to run a specific function in the user control program area. But when I go to define what the callback is in the “int main” program area, it is rejecting it. Could there be any solutions to my program?


I think you are defining your subroutine too low down. I believe it needs to be right under where it says “using namespace” Here’s a better example. I know this one works:

void lifter() {
  Motor1.spin(forward);
}
int main() {
  Controller1.ButtonR1.pressed(lifter);
}