UserControl Pressed Event Issue

Hey everyone, I have a usercontrol coding question. I have three pressed events in my usercontrol while loop (as seen below), but only 2 of them work at a time. Does anyone know the reason for this? When I comment one of the three out both of them seem to work so it can’t be a problem with the functions individually, lol I’m just lost.

“”"
Controller1.ButtonY.pressed(myFunction1);
Controller1.ButtonRight.pressed(myFunction2);
Controller1.ButtonA.pressed(myFunction3);
“”"

1 Like

It may be helpful for you to show us the functions. Or do a test program where the function prints on the display which function it is.

As @Foster mentions, it would be useful to see your full code. This comment indicates you are doing something like:

while(1) {
  Controller1.ButtonY.pressed(myFunction1);
  Controller1.ButtonRight.pressed(myFunction2);
  Controller1.ButtonA.pressed(myFunction3);
  // Other stuff
  wait(20, msec);
}

Which is not what you want to do. This will register a callback for the 3 buttons every 20 milliseconds. Callbacks only need to be registered once. You have several alternatives:

Controller1.ButtonY.pressed(myFunction1);
Controller1.ButtonRight.pressed(myFunction2);
Controller1.ButtonA.pressed(myFunction3);
while(1) {
  // Other stuff
  wait(20, msec);
}

This keeps the use of the event-based programming model (which I generally favor).

Or:

while(1) {
  if(Controller1.ButtonY.pressing() {
    myFunction1();
  }
  if(Controller1.ButtonRight.pressing() {
    myFunction2();
  }
  if(Controller1.ButtonA.pressing() {
    myFunction3();
  }
  // Other stuff
  wait(20, msec);
}

This method checks whether the button is currently being pressed and then calls your function. There are some potential consequences to this method (e.g. it’s not likely a human will release a button in 20 milliseconds) that you need to think through. There are times where the pressing() way is better than the event-based way, and other times where the event-based way is better.

6 Likes

Thanks so much that cleared up a lot for me, I didn’t realize the callbacks only needed to be called once, but looking at it now was very very dumb on my part lol. But hey, I guess our bot qualified for state with incorrect code.