VexCode

I have a problem with VexCode.
I declared a function and did a callback.
But it just doesn’t work somehow.

1 2

1 Like

More code/information would be helpful.

Also make sure that you aren’t setting the motor speed to something else in usercontrol function.

1 Like
Read the rest of the post first

void buttonA() needs to be declared before (above) your usage of it in Controller1.ButtonA.pressed().

In other words, you should add the line of code

void buttonA();

above what is line 161 in your screenshot.


On second examination, it is very unclear how your code is structured from the 2 unlabeled screenshots you posted, as @Codec seems to also have noticed. It is entirely possible that my declaration suggestion is not at all relevant to your problem.

If you upload your code to GitHub or a similar service, it will be much easier for the community to help you.

1 Like

Rather than putting that callback in main trump using an if(){} statement

If(buttonB.pressing()){whatever}

2 Likes

Thank you, I will try that.

pressing ( ) and pressed ( ) are different. And I want pressed ( ).

It is possible to use pressing() exclusively and avoid pressed() altogether, and still accomplish exactly the same outcome, which is a stylistic choice I would make.

pressing ( ) means you have to press the entire time.
pressed ( ) means you only have to press it once.

For what your doing both will do the same thing. Since it is fine to repeatedly set the motor speed, having if(buttonB.pressing()) {run motors} will repeatedly set the motor speed resulting in the same thing as just setting it once when the button is first pressed. The .pressed or.released callbacks are useful when you need to do something once or if you want to do something once the button is released.

It makes more sense to just say…

.pressing() returns true while the button is being pressed
.pressed() runs a callback once when the button is pressed
.released() runs a callback once when the button released

3 Likes

Pressed creates an event handler with some edge trigger logic. You can create the same edge trigger logic with a boolean and a loop:

bool buttonPressedLastLoop = false;
while (true) {
    if (button.pressing() && !buttonPressedLastLoop) {
        buttonPressedLastLoop = true;
        //stuff to do on rising edge condition goes here
    } else if (!button.pressing() && buttonPressedLastLoop) {
        buttonPressedLastLoop = false;
        //stuff to do on falling edge condition goes here
    }
}

That’s the guts of the logic that the event handler is doing to figure out if it should fire its pressed or released events.

But as for your actual problem, my money is on @Codec’s first idea that you’re sending conflicting motor commands from elsewhere in your program.

3 Likes