Code related to stick not working on first load of program, works on subsequent

I am having trouble with my code. The goal is to have the right stick’s vertical axis control how slow or fast a motor will spin. This is my code:

void RMSpin (){
  RollerMotor.setVelocity(abs(Controller1.Axis2.position() / n), percent);
  if(Controller1.Axis2.position() > 0){
  } else {

I have it being called with

if(Controller1.Axis2.position() != 0){

The error which I’m having is that when we first load the code up, the right stick will do nothing. However when I quit out and load it up again, the right stick will work as intended. It is also only the right stick that is having this issue. The drivetrain still works perfectly fine. Any ideas?

First of all your subroutine is not in a “while” loop. It needs to be in a while loop to run. Secondly, your code is definitely wrong you could make it much simpler by saying:

void RMSpin(){
RollerMotor.setVelocity(Controller1.Axis2.position(), percent);

this way, even if you are telling it to spin forward, but have a negative velocity number, the motor will spin reverse. I am also not sure about having it in a subroutine. Personally, I would put it inside your while loop that you use for driving.

That will register multiple events. Controller1.Axis2.changed(RMSpin) should only ever be called once to register the RMSpin function as an event hander when the Axis2 values changes.

But I don’t recommend using changed events really, keep it simple and just call RMSpin directly.