There’s an example of using pressed here
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.
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.
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.
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);
Im currently looking at another code, but ill give this a try.
When its in the function it does not have any effect on the user control
there should be a speed setting in the spin command.
I know that. What I meant was that our goal is to make the motors spin at different speeds through the use of a button being pressed once.
set a variable like
int speed=0 ;/*always have a initial value for variables or there will be trouble */
and in your operator control loop
while(1)
{
speed= Controller1.ButtonX.pressed()? speed+1:speed;
speed= speed>=4? 0:speed;
switch (speed)
{
case 0: motor.spin(directionType::fwd,50,velocityUnits::pct);
case 1: motor.spin(directionType::fwd,70,velocityUnits::pct);
case 2: motor.spin(directionType::fwd,90,velocityUnits::pct);
case 3: motor. stop(brakeType:hold) ;
}
}
edit : @enothecool