.pressed() is running multiple times on one press

Hello! Basically, my code is running multiple times for one call.

This is my called function

void r1p(){
  r1var += 1;
  cout << r1var << endl;
}
 Controller1.ButtonR1.pressed(r1p);

Yet when I press R1 once (not held), the variable r1var iterates 18 times, and gets printed 18 times.
The length of how long I hold the button does not affect the times, but it always specifically runs 18 times.
It does not seem to be a controller issue, as the same thing happens with a different controller.

I’ve temporarily solved this issue with

if((r1var / 18)% 2 == 0){
      flMotor.spin(vex::directionType::fwd,0,vex::voltageUnits::volt);
    } else{
      flMotor.spin(vex::directionType::fwd, 8.5,vex::voltageUnits::volt);
      //flMotor.spin(vex::directionType::fwd,6.2,vex::voltageUnits::volt);
    }
}

It works, but now I am just curious on why it runs such a specific number of times. I am in VEXcode.

make sure you have only called that once in the code, that is, make sure it’s not inside a while loop. Event registration must only happen once.

4 Likes

It’s inside the main while loop. Is this the issue? Where should I put it if I shouldn’t put it in the main? And I have only called it once.

yes, that’s an issue. move it to the beginning of main or somewhere else it will only be called once.

4 Likes

Oh ok. is there a reason for the specific 18 number then?

you are registering the same event handler (the r1p function) many times, eventually the V5 runs out of resources to add any more events. The V5 can handle many more event handlers than 18 but perhaps you have other event handlers being registered multiple times as well, idk. So “18” doesn’t have any special meaning other than meaning you probably registered 18 event handlers for the R1 button.

4 Likes