Hello motor won't spin

Hello, my motor won’t spin. It will be in in the middle of a program and just not spin any idea how to fix it?

Gonna need some specifics there, chief. What’s your code? Is the motor warm, anything else we need know?

4 Likes

The motor isn’t warm the code is just Motor.spin task::sleep(1000)
and it won’t spin

Can you post your ENTIRE code file. You can use 3 backticks like this:

< ```>

Copy code here

<```>

Disregard the <'s

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// LineTrackerL         line          A               
// LineTrackerM         line          B               
// LineTrackerR         line          C               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"
#include <vex_triport.h>
#include <cmath>
using namespace vex;

// A global instance of competition
competition Competition;
brain brain1;
vex::motor ClawMotor = vex::motor(vex::PORT5);
vex::motor LeftMotor = vex::motor(vex::PORT2);
vex::motor RightMotor = vex::motor(vex::PORT14);
vex::motor Motor1 = vex::motor(vex::PORT4, true);
vex::motor Motor2 = vex::motor(vex::PORT19, true);
vex::controller Controller1 = vex::controller();

double sgn(double number) {
  if (number > 0)
    number = 1;
  else
    number = -1;
  return number;
}

void close_claw(){
  ClawMotor.spin(directionType::fwd, 15, velocityUnits::pct);
  task::sleep(750);
  while (ClawMotor.velocity(pct) > 0) {
    ClawMotor.spin(directionType::fwd, 50, velocityUnits::pct);
  }
  ClawMotor.stop(brakeType::hold);
}

void setLeftPower(int power) {
  if (power == 0)
    LeftMotor.stop(vex::brakeType::brake);
   else {
    LeftMotor.spin(vex::directionType::fwd, power, vex::velocityUnits::pct);
  }
}
void setRightPower(int power) {
  if (power == 0)
    RightMotor.stop(vex::brakeType::brake);
   else {
    RightMotor.spin(vex::directionType::rev, power, vex::velocityUnits::pct);
  }
}

void setDrivePower(int left, int right) {
  setLeftPower(left);
  setRightPower(right);
}

void driveStraight(int distance, int power = 100) {
  int direction = sgn(distance);

  // Clear encoder
  LeftMotor.resetRotation();
  RightMotor.resetRotation();

  while (std::abs(LeftMotor.rotation(vex::rotationUnits::deg)) < std::abs(distance)) {
    setDrivePower(power * direction, power * direction);
  }
  setDrivePower(0, 0);
}
void printLineTrackerValue()
{
  Brain.Screen.clearScreen();
  Brain.Screen.setFont(monoS);
  Brain.Screen.print(Brain.Timer.value());
  while(true)
  {
    task::sleep(3000);
    Brain.Screen.newLine();
    Brain.Screen.print(LineTrackerL.value(analogUnits::range12bit));
    Brain.Screen.newLine();
    Brain.Screen.print(LineTrackerM.value(analogUnits::range12bit));
    Brain.Screen.newLine();
    Brain.Screen.print(LineTrackerR.value(analogUnits::range12bit));
  }
}

// if pass 50, means 50% power
void driveForwardByVelocity(double velocity) {
  LeftMotor.spin(directionType::fwd, velocity, vex::velocityUnits::pct);
  RightMotor.spin(directionType::rev, velocity, vex::velocityUnits::pct);
}

bool isZeroValues() 
{
  if (LineTrackerL.value(analogUnits::range12bit) == 0 &&
      LineTrackerM.value(analogUnits::range12bit) == 0 && 
      LineTrackerR.value(analogUnits::range12bit) == 0 ) {
     return true;
  }
  return false; 
}

void hit_wall(double velocity_left, double velocity_right, int sleep_time){
    LeftMotor.spin(directionType::rev, velocity_left, velocityUnits::pct);
   RightMotor.spin(directionType::fwd, velocity_right, velocityUnits::pct);
   task::sleep(sleep_time);
   while (LeftMotor.velocity(pct) > 0 && RightMotor.velocity(pct)> 0) {
    LeftMotor.spin(directionType::rev, velocity_left, velocityUnits::pct);
    RightMotor.spin(directionType::fwd, velocity_right, velocityUnits::pct);
  }
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
}

void open_claw(){
 ClawMotor.spin(directionType::rev, 70, velocityUnits::pct);
  task::sleep(500);
  while (ClawMotor.velocity(pct) > 0) {
    ClawMotor.spin(directionType::rev, 70, velocityUnits::pct);
  }
  ClawMotor.stop(brakeType::hold); 
}

void see_light_stop(double velocityl, double velocityr) {
  while (true) {
      //driveForwardByVelocity(40);
      LeftMotor.spin(directionType::fwd, velocityl, velocityUnits::pct);
      RightMotor.spin(directionType::rev, velocityr, velocityUnits::pct);
      
      if (!isZeroValues() &&
          (LineTrackerL.value(analogUnits::range12bit) < 1500 ||
            LineTrackerM.value(analogUnits::range12bit) < 1500 ||
            LineTrackerR.value(analogUnits::range12bit) < 1500) ){
      break;
    }
  }
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
}

void see_dark_stop(double velocityl, double velocityr) {
  while (true) {
      //driveForwardByVelocity(40);
      LeftMotor.spin(directionType::fwd, velocityl, velocityUnits::pct);
      RightMotor.spin(directionType::rev, velocityr, velocityUnits::pct);
      
      if (!isZeroValues() &&
          (LineTrackerL.value(analogUnits::range12bit) > 1500 ||
           LineTrackerM.value(analogUnits::range12bit) > 1500 ||
           LineTrackerR.value(analogUnits::range12bit) > 1500) ){
      break;
    }
  }
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
}

void back_see_light_stop(double velocityl, double velocityr) {
  while (true) {
      //drive Back
      LeftMotor.spin(directionType::rev, velocityl, velocityUnits::pct);
      RightMotor.spin(directionType::fwd, velocityr, velocityUnits::pct);

      if (!isZeroValues() &&
          (LineTrackerL.value(analogUnits::range12bit) < 1500 ||
            LineTrackerM.value(analogUnits::range12bit) < 1500 ||
            LineTrackerR.value(analogUnits::range12bit) < 1500) ){
      break;
    }
  }
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
}

void back_see_dark_stop(double velocityl, double velocityr) {
  while (true) {
      //drive Back
      LeftMotor.spin(directionType::rev, velocityl, velocityUnits::pct);
      RightMotor.spin(directionType::fwd, velocityr, velocityUnits::pct);

      if (!isZeroValues() &&
          (LineTrackerL.value(analogUnits::range12bit) > 1500 ||
            LineTrackerM.value(analogUnits::range12bit) > 1500 ||
            LineTrackerR.value(analogUnits::range12bit) > 1500) ){
      break;
    }
  }
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
}

//trun right: autoTurn(100), slower Turn(100, 30)
void turn_right(int distance, int power) {
  int direction = sgn(distance);

  // Clear encoder
  LeftMotor.resetRotation();
  RightMotor.resetRotation();

  while (std::abs(LeftMotor.rotation(vex::rotationUnits::deg)) <std::abs(distance)) {
    setDrivePower(power * direction, -power * direction);
  }
  setDrivePower(0, 0);
}

//trun left: autoTurn(100), slower Turn(100, 30)
void turn_left(int distance, int power) {
  int direction = sgn(distance);

  // Clear encoder
  LeftMotor.resetRotation();
  RightMotor.resetRotation();

  while (std::abs(LeftMotor.rotation(vex::rotationUnits::deg)) <std::abs(distance)) {
    setDrivePower(-power * direction, power * direction);
  }
  setDrivePower(0, 0);
}

//lower arm before call this
//grab one cube and drop to a tower
void grab_onecube_tower() {
  driveStraight(300, 40);
  close_claw();

  //move back
  driveStraight(-75, 40);

  //raise claw
  Motor1.setVelocity(100, vex::velocityUnits::pct);
  Motor2.setVelocity(100, vex::velocityUnits::pct);
  Motor1.rotateFor(-3, vex::rotationUnits::rev, false);
  Motor2.rotateFor(3, vex::rotationUnits::rev);
  
  driveStraight(200, 20);

  //drop cube
  ClawMotor.spin(directionType::rev, 70, velocityUnits::pct);
  task::sleep(500);
  while (ClawMotor.velocity(pct) > 0) {
    ClawMotor.spin(directionType::rev, 70, velocityUnits::pct);
  }
  ClawMotor.stop(brakeType::hold); 

  //drive back
  driveStraight(-50, 10);
}

//do one tower and put five cubes in blue base
void part_one(){
  ClawMotor.spin(directionType::fwd);
  task::sleep(300);
  //raise arm
  Motor1.setVelocity(100, vex::velocityUnits::pct);
  Motor2.setVelocity(100, vex::velocityUnits::pct);
  Motor1.rotateFor(-3, vex::rotationUnits::rev, false);
  Motor2.rotateFor(3, vex::rotationUnits::rev);
  //turn and go foward
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);

  LeftMotor.rotateFor(-0.85, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-0.85, vex::rotationUnits::rev);

  LeftMotor.rotateFor(0.4, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-0.4, vex::rotationUnits::rev);
  // drop cube
  open_claw(); 

  //go back and hit the wall
  hit_wall(30,50,1000);

  //go straight, lower claw, close claw
  LeftMotor.setVelocity(25, vex::velocityUnits::pct);
  RightMotor.setVelocity(25, vex::velocityUnits::pct);
 
  LeftMotor.rotateFor(0.8, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-0.8, vex::rotationUnits::rev);
  
  Motor1.setVelocity(50, vex::velocityUnits::pct);
  Motor2.setVelocity(50, vex::velocityUnits::pct);

  Motor1.rotateFor(3, vex::rotationUnits::rev, false);
  Motor2.rotateFor(-3, vex::rotationUnits::rev, false);
  
  //open claw
  open_claw();
  
  //do shallow turn
  LeftMotor.setVelocity(25, vex::velocityUnits::pct);
  RightMotor.setVelocity(20, vex::velocityUnits::pct);
  LeftMotor.rotateFor(1.5, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.2, vex::rotationUnits::rev, false);
 
  //do sharp turn
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);
  LeftMotor.rotateFor(0.85, vex::rotationUnits::rev);

  //go foward
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);
  LeftMotor.rotateFor(1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1, vex::rotationUnits::rev);

  //go foward slowly
  LeftMotor.setVelocity(10, vex::velocityUnits::pct);
  RightMotor.setVelocity(10, vex::velocityUnits::pct);
  LeftMotor.rotateFor(1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1, vex::rotationUnits::rev);
  
  close_claw();
  
  //clamp claw
  ClawMotor.rotateFor(1, vex::rotationUnits::rev, false);

  //lower claw down
  Motor1.setVelocity(100, vex::velocityUnits::pct);
  Motor2.setVelocity(100, vex::velocityUnits::pct);
 
  Motor1.spin(directionType::fwd);
  Motor2.spin(directionType::rev);
  task::sleep(300);
 
  //raise claw
  Motor1.setVelocity(35, vex::velocityUnits::pct);
  Motor2.setVelocity(35, vex::velocityUnits::pct);

  Motor1.rotateFor(-1.2, vex::rotationUnits::rev, false);
  Motor2.rotateFor(1.2, vex::rotationUnits::rev);
  
  LeftMotor.rotateFor(0.4, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(0.4, vex::rotationUnits::rev);

  //back up
  LeftMotor.rotateFor(-0.3, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(0.3, vex::rotationUnits::rev);
  //clamp claw
  ClawMotor.rotateFor(3, vex::rotationUnits::rev, false);
  //turn around
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);

  LeftMotor.rotateFor(1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(1, vex::rotationUnits::rev);
  //go foward
  LeftMotor.rotateFor(1.2, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.2, vex::rotationUnits::rev);
  
  //turn
  LeftMotor.setVelocity(20, vex::velocityUnits::pct);
  RightMotor.setVelocity(60, vex::velocityUnits::pct);
  LeftMotor.rotateFor(0.7, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.4, vex::rotationUnits::rev);
  
  //drive into base
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);  

  LeftMotor.spin(directionType::fwd, 50, velocityUnits::pct);
  RightMotor.spin(directionType::rev, 50, velocityUnits::pct);
  task::sleep(1500); 
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
  
  open_claw();
  //back out
  LeftMotor.rotateFor(-1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(1, vex::rotationUnits::rev);

  close_claw();
}

//put five cubes in blue base
void part_two() {
  //back out more
  LeftMotor.rotateFor(-2, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(2, vex::rotationUnits::rev);
  //turn
  LeftMotor.setVelocity(30, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);

  LeftMotor.rotateFor(1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.5, vex::rotationUnits::rev);

  //turn again
  LeftMotor.rotateFor(1.3, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.6, vex::rotationUnits::rev);
  //turn again
  LeftMotor.setVelocity(40, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);

  LeftMotor.rotateFor(1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-1.4, vex::rotationUnits::rev);

  driveStraight(1250, 100);
 
  see_light_stop(30, 30);

  //back up
  LeftMotor.rotateFor(-0.1, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(0.1, vex::rotationUnits::rev);
  
  //turn
  LeftMotor.rotateFor(-0.8, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(-0.8, vex::rotationUnits::rev);
  
  //hit the wall
  hit_wall(50,50,1000);

  //open claw and lower arm
  ClawMotor.rotateFor(-1, vex::rotationUnits::rev, false);
  Motor1.rotateFor(1, vex::rotationUnits::rev, false);
  Motor2.rotateFor(-1, vex::rotationUnits::rev, false);

  driveStraight(700, 50);
  driveStraight(500, 10);

  //grab stack
  close_claw();
  
  //clamp claw
  ClawMotor.rotateFor(1,vex::rotationUnits::rev,false);
  
  //lower claw
  Motor1.spin(directionType::fwd);
  Motor2.spin(directionType::rev);
  task::sleep(500);
  
  //raise claw
  Motor1.rotateFor(-1.3, vex::rotationUnits::rev, false);
  Motor2.rotateFor(1.3, vex::rotationUnits::rev);
  
  //turn
  LeftMotor.rotateFor(0.95, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(0.95, vex::rotationUnits::rev);

  driveStraight(900, 30);
  
  LeftMotor.setVelocity(50, vex::velocityUnits::pct);
  RightMotor.setVelocity(50, vex::velocityUnits::pct);
  //turn
  RightMotor.rotateFor(0.2, vex::rotationUnits::rev, false);
  LeftMotor.rotateFor(1, vex::rotationUnits::rev);
  
  //drive into base
  LeftMotor.spin(directionType::fwd, 50, velocityUnits::pct);
  RightMotor.spin(directionType::rev, 50, velocityUnits::pct);
  task::sleep(750);
  LeftMotor.stop(brakeType::hold);
  RightMotor.stop(brakeType::hold);
  
  see_dark_stop(30, 30);
  driveStraight(200,50);

  //sleep 500 to prevent cubes stack callapse
  task::sleep(500);

  //drop cubes
  open_claw();

  LeftMotor.rotateFor(-2, vex::rotationUnits::rev, false);
  RightMotor.rotateFor(2, vex::rotationUnits::rev);
}

//put cube to tower after drive back from base. 
//pass true for blue base, pass false for red base.
void tower_from_base(bool blueBase) {
  //back from base, see white line and stop
  back_see_light_stop(40, 40);

  driveStraight(200, 20);

  //close_claw();
  ClawMotor.spin(directionType::fwd, 50, velocityUnits::pct);
  task::sleep(500);
  while (ClawMotor.velocity(pct) > 0) {
    ClawMotor.spin(directionType::fwd, 80, velocityUnits::pct);
  }

  Motor1.setVelocity(100, vex::velocityUnits::pct);
  Motor2.setVelocity(100, vex::velocityUnits::pct);
  Motor1.spin(directionType::fwd);
  Motor2.spin(directionType::rev);
  task::sleep(100);

  //if blue base, turn left. Red base, turn right
  if(blueBase) {
    turn_left(270, 40);
  } else {
    turn_right(270, 40);
  }
  
  hit_wall(50, 50, 1000);

  //open claw and lower arm
  ClawMotor.rotateFor(-1, vex::rotationUnits::rev, false);
  Motor1.rotateFor(0.8, vex::rotationUnits::rev, false);
  Motor2.rotateFor(-0.8, vex::rotationUnits::rev, false);

  driveStraight(1000, 45);

  grab_onecube_tower();
}


// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous(void) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ..........................................................................
  

  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  part_one();
  part_two();
 // printLineTrackerValue();

 //todo: we still have five seconds leftover, 
 //and have the potential of getting another tower
  tower_from_base(false);
}



/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              User Control Task                            */
/*                                                                           */
/*  This task is used to control your robot during the user control phase of */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void usercontrol(void) {
  // User control code here, inside the loop
  while (1) {
      while (true) {
    if (Controller1.ButtonR1.pressing()){
         LeftMotor.spin (directionType::fwd,(Controller1.Axis1.value() + Controller1.Axis2.value())/10, velocityUnits::pct); 
         RightMotor.spin(directionType::fwd,(Controller1.Axis1.value() - Controller1.Axis2.value())/10, velocityUnits::pct);
         }
         //following codes: speed setting two
         else if (Controller1.ButtonR2.pressing()){
         LeftMotor.spin(directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis2.value())/1, velocityUnits::pct);
         RightMotor.spin(directionType::fwd,(Controller1.Axis1.value() - Controller1.Axis2.value())/1, velocityUnits::pct);
         }
         //following codes: speed setting three
         else{
         LeftMotor.spin (directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis2.value())/2.5, velocityUnits::pct); 
         RightMotor.spin(directionType::fwd, (Controller1.Axis1.value() - Controller1.Axis2.value())/2.5, velocityUnits::pct);
         }


         Motor1.spin    (directionType::fwd, (Controller1.Axis4.value() - Controller1.Axis3.value())/1, velocityUnits::pct);
         Motor2.spin    (directionType::fwd, (Controller1.Axis4.value() + Controller1.Axis3.value())/1, velocityUnits::pct);
         
         //following codes control clawmotor motion
         if (Controller1.ButtonL1.pressing()){
           ClawMotor.spin(directionType::fwd, 70, velocityUnits::pct);
         }
         else if(Controller1.ButtonL2.pressing()){
           ClawMotor.spin(directionType::rev, 70, velocityUnits::pct);
         }
         else{
           ClawMotor.stop(brakeType::hold);
         }
     
        }
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

the claw motor won’t spin

You usecrontrol function looks fine to me, except that you’ve put an infinite loop in an infinite loop, which is unnecessary and means the call to wait will never run since it’s after the inner loop.

Some additional diagnostic questions:

  • Will the motor spin if you try to run it from other parts of the program?
  • Can you run the motor from the on-brain devices screen?
3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.