Callbacks

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.

Post the code so we can see what you are trying to do.

Y=50 would not be a callback, it’s a variable. I think your variable is declared inside a different function than usercontrol, which means that it will only be recognized when you reference it in that function. Put the declaration outside of any function to make it a global variable, which you can call on from anywhere in the code.

OurCode.zip (11.4 KB)
Theres our code

I tried putting it outside of the function and it worked, but how would I change the variable?

A global variable can be accessed by all functions and all threads. You declare a local variable inside this function.

void function(){
  int y=50;
}

when using global variables it’s best to give them a descriptive name, so if this variable will represent, for example, a speed, perhaps name it.

int liftSpeed;

or if you want an initial value for it

int liftSpeed = 15;

place that line way at the top of main outside of any functions, perhaps under this line of code.

vex::competition    Competition;

then, in your function you can set it like this.

 void function(){
   liftSpeed  = 50;
}

and use it later like this.

int DownSpeedPCT = liftSpeed;

(although there’s no real point doing that copy, you could just use it in here directly by replacing DownSpeedPCT with liftSpeed.

          LA.spin(vex::directionType::fwd, DownSpeedPCT, vex::velocityUnits::pct);
2 Likes