6 motor drive code not working

Hello, whenever I move either joystick on my code, the wheels don’t stop moving even when I let go of the joystick. If someone can figure out the problem, that would be great. Thank you.

void usercontrol(void) {
 // User control code here, inside the loop
 while (1) {
   FrontL.setVelocity(600, rpm);
   MiddleL.setVelocity(600, rpm);
   BackL.setVelocity(600, rpm);
   FrontR.setVelocity(600, rpm);
   MiddleR.setVelocity(600, rpm);
   BackR.setVelocity(600, rpm);


   if (Controller1.Axis3.value() > 0)
 {FrontL.spin(forward);
 MiddleL.spin(forward);
 BackL.spin(forward);
 FrontR.spin(forward);
 MiddleR.spin(forward);
 BackR.spin(forward);
 }


 else(Controller1.Axis3.value() ==0)
 {FrontL.stop();
 MiddleL.stop();
 BackL.stop();
 FrontR.stop();
 MiddleR.stop();
 BackR.stop();
 }


 if (Controller1.Axis3.value() < 0)
 {FrontL.spin(reverse);
 MiddleL.spin(reverse);
 BackL.spin(reverse);
 FrontR.spin(reverse);
 MiddleR.spin(reverse);
 BackR.spin(reverse);
 }


 else(Controller1.Axis3.value() ==0)
 {FrontL.stop();
 MiddleL.stop();
 BackL.stop();
 FrontR.stop();
 MiddleR.stop();
 BackR.stop();
 }


  if (Controller1.Axis1.value() < 0)
 {FrontL.spin(forward);
 MiddleL.spin(forward);
 BackL.spin(forward);
 FrontR.spin(reverse);
 MiddleR.spin(reverse);
 BackR.spin(reverse);
 }


 else (Controller1.Axis1.value() ==0)
 {FrontL.stop();
 MiddleL.stop();
 BackL.stop();
 FrontR.stop();
 MiddleR.stop();
 BackR.stop();
 }


  if (Controller1.Axis1.value() > 0)
 {FrontL.spin(reverse);
 MiddleL.spin(reverse);
 BackL.spin(reverse);
 FrontR.spin(forward);
 MiddleR.spin(forward);
 BackR.spin(forward);
 }
else (Controller1.Axis1.value() ==0)
 {FrontL.stop();
 MiddleL.stop();
 BackL.stop();
 FrontR.stop();
 MiddleR.stop();
 BackR.stop();
 }




`

You aren’t supposed to put a condition in an else statement. For that, you would use else if (or just else without a condition (no parenthesis) , but that would also include if the axes had the opposite value that you are checking for, so I suggest using else if to check both positive then negative (and spin the motors accordingly) then an else to stop the motors if it is neither positive or negative (in other words, if it is 0) ). I hope this helps!

Also, the current version of the drive code goes full speed if the joystick goes up, stops if it’s exactly zero, and does nothing if the value is less than zero. The better version of this is to spin the motors with the joystick value speed as this allows better control and will take care of reversing.

Vex had example programs that may help you. Below is one example that I have modified with pseduo-code.

int main() {
  // Begin project code.
  // Main Controller loop to set motors to controller axis postiions
  LeftMotor.spin(forward);
  //Add additional left motors
  RightMotor.spin(forward);
  //Add additional right motors

  while (true) {
    LeftMotor.setVelocity((Controller1.Axis2.position() + Controller1.Axis1.position()), percent);
    //Left2.serVelocity(..);
    //Left3.serVelocity(..);
    RightMotor.setVelocity((Controller1.Axis2.position() - Controller1.Axis1.position()), percent);
    //Right2.serVelocity(..);
    //Right3.serVelocity(..);

    wait(5, msec);
  }

  return 0;
}
1 Like