Spark Max code does not working properly

We defined intake as

  PWMSparkMax intake = new PWMSparkMax(0);

and powered it with this code:

    if (controller.getXButtonPressed()){
      intake.set(0.5);
      System.out.println("Button Pressed X");}
    if (controller.getXButtonReleased()){
      intake.set(0);
    }

but this code only runs motor for like 20 miliseconds and powers it again when we pressed X again.

You’re only running the motor on the single iteration when the button switches from false to true.

Consider using the command-based framework, which will help with scheduling tasks and remove the need to use explicit if statements in the main loop.

I dont have any idea about command-based, yet i dont think i can learn it till regional. We have same part in our code which works perfectly:

   if (controller.getYButtonPressed()){
      shooter1.set(-1);
      shooter2.set(1);
      System.out.println("Button Pressed Y");}
    if (controller.getYButtonReleased()){
      shooter1.set(0);
      shooter2.set(0);
    }

shooter1 and shooter2 defined like:

  VictorSP shooter1 = new VictorSP(1);
  VictorSP shooter2 = new VictorSP(2);

Maybe try using getXButton() instead of getXButtonPressed()? This should return True as long as the button is held down.

Alright, we handled it (it turns out we didn’t plug NEO encoder cable).

1 Like