Callbacks

So pretty much on our code I need to used the pressed command, and it says I need a callback. Anyone know what it is/how I can implement it? I will post my code if needed.

The pressed function will, when the button/switch is pressed, go through a function (the callback). An example of this:
Controller1.ButtonRight.pressed(function)
Controller1.ButtonRight is the switch you are reading, and function is the function.

Nice
Now I just need to ask what a function is?

I’m pretty new to this stuff to, but I tend to learn from editing others code to match what I need until I understand it enough to make my own. This is some of my code that might help you.

Also it appears that I don’t know how to put this as a code block. Oops.

if(Controller1.ButtonR1.pressing() &! ArmUpperLimit.pressing()){
ArmMotor.spin(directionType::rev,100,velocityUnits::pct);
}
else if(Controller1.ButtonR2.pressing()){
ArmMotor.spin(directionType::fwd,100,velocityUnits::pct);
}
else{
ArmMotor.stop(brakeType::hold);
}

This basically says that if R1 is pressed and the upper arm limit switch is NOT pressed, it will spin the arm motor.

From what I understand, the function is basically like asking yes or no or something, so the parenthesis are actually left empty, and when the code is run a 0 or 1 boolean variable is inserted and then the if statement decides what happens for that 1 or 0. <–All that might be wrong to. xD

Im not entirely sure that’s the fix I’m looking for. Its the pressing command not the pressed one, so it doesn’t really do what we want. Though I do suppose we could code a second controller use pressing if we weren’t too lazy to ask other teams for their controller during matches lol

Ah yeah I tried it myself and found that error, my bad lol. I don’t think 2 controllers is a good solution either lol. However, this example should answer your question. How do I use the controller button pressed command

Thanks that explains it

No problem.
20 characters

Im making headway on our code, but this error is appearing
[clang] Expected ‘(’ for function-style cast or type construction
to this
29%20AM
Anyone know why?

also you don’t have to put the basic commands into custom functions if it’s just one or two of them. just put the motor. spin command or something in the parenthesis and it should work. They are functions as well after all, just defined in the vex name space. i don’t know if that’s entirely correct though. please correct me if I’m wrong which is quite likely:sweat_smile:

There’s an example of using pressed here

2 Likes

well our goal is to change the motor speed not have motors moving

jpearman,

Why does the “callback_pressed” function get executed more than one time? Also the number of times it runs the callback is dependent on the number of callbacks defined:

Try this code:

#include “vex.h”
using namespace vex;

vex::brain Brain;
vex::controller Controller1 = vex::controller();

int xcounter = 0;
int ycounter = 0;
int acounter = 0;
int bcounter = 0;

void x_pressed()
{
xcounter ++;
Brain.Screen.printAt(100, 20, “X was pressed %d times”, xcounter);
}

void y_pressed()
{
ycounter ++;
Brain.Screen.printAt(100, 60, “Y was pressed %d times”, ycounter);
}

void a_pressed()
{
acounter ++;
Brain.Screen.printAt(100, 100, “A was pressed %d times”, acounter);
}

void b_pressed()
{
bcounter ++;
Brain.Screen.printAt(100, 140, “B was pressed %d times”, bcounter);
}

int main() {
while(1) {
Controller1.ButtonX.pressed(x_pressed);
Controller1.ButtonY.pressed(y_pressed);
Controller1.ButtonA.pressed(a_pressed);
Controller1.ButtonB.pressed(b_pressed);
vex::task::sleep(10);
}
}

It should make the counters increase by 1 each time the appropriate button is pushed, however they increase by 24 (and 25 for the X button). If there are less callbacks defined the number of repeats of each callback goes up.

Why is the callback being called and ran over and over again?

Paul

This should be outside of the while loop. (that is, before the while loop at the top of main)

The idea here is that you are registering a function (the callback) that will be executed when the event fires. Having those lines inside the while loop is registering the function multiple times ( we eventually run out of internal event resources )

This

Controller1.ButtonX.pressed(x_pressed);

means register the function x_pressed, call it every time the button X is pressed.

we have “pressed” events, “released” events and a few others like “changed” that will be fired when sensors such as gyros and potentiometers are moved, ie. their value is changed.

Yes apparently there is a total of 97 possible registrations before it runs out of resources… as the total of all of the callbacks always adds up to 97.

Thanks for the quick reply.

Paul

yea, I set the limit at 100 events. Some are registered when you create, for example, an instance of a competition object. If you have some tasks, that limit is reduced, limit for tasks is 128, but as an event also runs as a task it uses resources for both.

2 Likes

One more question to ask you guys then it should be working. Do you know how to set up variables?

Just to clarify y=15 kind of stuff

int main() {
    int y = 15;       // integer variable, whole number 1, 2, 3 etc.
    double d = 3.14;  // floating point type
    etc.
}

see here

and here
https://www.tutorialspoint.com/cprogramming/c_variables.htm

there are many, many examples on the internet.
search for C programming tutorials and things like that.

2 Likes

Thanks
On my code its not identifying y in the user control area. Any ideas on how I can get it to do this? I have a callback for y=50, but I don’t know why it won’t register in the user control area.