Computer keeps skipping code

Hi guys,

I’m trying to write a selector for the color of ball that will be ejected by the pooper using a potentiometer. I works by using a boolean, poopColor, that is set to true for ejecting red balls and blue for ejecting blue balls. However, the potentiometer keeps returning a value of 10 and nothing else. Does anyone know the cause of this issue?

Halp ples, thanks

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\Justin                                           */
/*    Created:      Fri Feb 05 2021                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// LeftFrontDrive       motor         1               
// LeftRearDrive        motor         2               
// RightFrontDrive      motor         3               
// RightRearDrive       motor         4               
// LeftIntake           motor         5               
// RightIntake          motor         6               
// Controller1          controller                    
// TopMotor             motor         7               
// BottomMotor          motor         8               
// ballSorter           optical       9               
// ejectorSelector      pot           A               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;
competition Competition;

double pi = 3.149265359; //creates a pi variable

//Declaring all movement functions
//---------------------------------------------------------------------------------------------------------------------------
void spinDrivetrain(void){ //moves drivetrain motors for an undetermined distance
  LeftFrontDrive.spin(vex::directionType::fwd , Controller1.Axis3.value() + Controller1.Axis1.value() , velocityUnits::pct);
  LeftRearDrive.spin(vex::directionType::fwd , Controller1.Axis3.value() + Controller1.Axis1.value() , velocityUnits::pct);
  RightFrontDrive.spin(vex::directionType::fwd , Controller1.Axis3.value() - Controller1.Axis1.value() , velocityUnits::pct);
  RightRearDrive.spin(vex::directionType::fwd , Controller1.Axis3.value() - Controller1.Axis1.value() , velocityUnits::pct);
}
void spinManipulatorsFwd(void){ //spins the intake and magazine motors forward
  RightIntake.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
  LeftIntake.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
  TopMotor.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
  BottomMotor.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
}
void spinManipulatorsRev(void){ //spins intake and magazine motors backward
  RightIntake.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  LeftIntake.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  TopMotor.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  BottomMotor.spin(vex::directionType::rev , 100 , velocityUnits::pct);
}
void spinMagazineFwd(void){ //spins magazine forwards
  TopMotor.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
  BottomMotor.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
}
void spinMagazineRev(void){ //spins magazine backwards
  TopMotor.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  BottomMotor.spin(vex::directionType::rev , 100 , velocityUnits::pct);
}
void spinIntakesFwd(void){
  RightIntake.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
  LeftIntake.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
}
void spinIntakesRev(void){
  RightIntake.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  LeftIntake.spin(vex::directionType::rev , 100 , velocityUnits::pct);
}
void stopDrivetrain(void){ //stops drivetrain w/ brake Type coast
  LeftFrontDrive.stop(coast);
  LeftRearDrive.stop(coast);
  RightFrontDrive.stop(coast);
  RightRearDrive.stop(coast);
}
void brakeDrivetrain(void){ //stops drivetrain with brake Type brake
  LeftFrontDrive.stop(brake);
  LeftRearDrive.stop(brake);
  RightFrontDrive.stop(brake);
  RightRearDrive.stop(brake);
}
void stopManipulators(void){ //stops all manipulators
  TopMotor.stop(coast);
  BottomMotor.stop(coast);
  RightIntake.stop(coast);
  LeftIntake.stop(coast);
}

void poop(void){
  TopMotor.spin(vex::directionType::rev , 100 , velocityUnits::pct);
  BottomMotor.spin(vex::directionType::fwd , 100 , velocityUnits::pct);
}


double distanceToTurns(double Distance){ //converts distance IN INCHES to the number of turns needed to reach that distance.
  double pi = 3.149265359;
  double circumference = 4 * pi;
  double rotations = Distance/circumference;
  return rotations;
}
bool robotStuck(){ //checks to see if the robot is stuck by comparing whether or not a motor is spinning with whether a motor is supposed to be spinning
  bool isStuck = false;
  if(LeftFrontDrive.isDone() != true && LeftFrontDrive.isSpinning() != true){
    wait(0.5 , seconds);
    if(LeftFrontDrive.isDone() != true && LeftFrontDrive.isSpinning() != true){
      isStuck = true;
    }
  }
  if(LeftRearDrive.isDone() != true && LeftRearDrive.isSpinning() != true){
    wait(0.5 , seconds);
    if(LeftRearDrive.isDone() != true && LeftRearDrive.isSpinning() != true){
      isStuck = true;
    }
  }
  if(RightFrontDrive.isDone() != true && RightFrontDrive.isSpinning() != true){
    wait(0.5 , seconds);
    if(RightFrontDrive.isDone() != true && RightFrontDrive.isSpinning() != true){
      isStuck = true;
    }
  }
  if(RightRearDrive.isDone() != true && RightRearDrive.isSpinning() != true){
    wait(0.5 , seconds);
    if(RightRearDrive.isDone() != true && RightRearDrive.isSpinning() != true){
      isStuck = true;
    }
  }
  return isStuck;
}
void setDrivetrainSpeed(double Velocity){ //set the motor speed as a percentage out of 100%
  Brain.Screen.print("setDrivetrainSpeed called");
  Brain.Screen.newLine();
  LeftRearDrive.setVelocity(Velocity , pct);
  LeftFrontDrive.setVelocity(Velocity , pct);
  RightRearDrive.setVelocity(Velocity , pct);
  RightFrontDrive.setVelocity(Velocity , pct);
}
void moveDrivetrainFor(double Distance){ //moves drivetrain a certain distance IN INCHES
  LeftFrontDrive.rotateFor(distanceToTurns(Distance) , rev , false);
  LeftRearDrive.rotateFor(distanceToTurns(Distance) , rev , false);
  RightRearDrive.rotateFor(distanceToTurns(Distance) , rev , false);
  RightFrontDrive.rotateFor(distanceToTurns(Distance) , rev , true);
  if(robotStuck() == true){ //if robot is stuck then wait 0.25 seconds
    wait(0.25 , seconds);
    if(robotStuck() == true){ //if robot is still stuck, then stop drivetrain
      brakeDrivetrain();
    }
  }
  stopDrivetrain();
  wait(1 , seconds);
}
void turnDrivetrainFor(double degrees){ //turns the drivetrain a set number of degrees, with the degrees increasing in a counterclockwise direction
  double robotCircumference = 17.5 * pi; //converts the robot's radius into a circle of x circumference
  LeftFrontDrive.rotateFor(-1 * distanceToTurns(degrees * robotCircumference / 360) , rev , false);
  LeftRearDrive.rotateFor(-1 * distanceToTurns(degrees * robotCircumference / 360) , rev , false);
  RightRearDrive.rotateFor(distanceToTurns(degrees * robotCircumference / 360) , rev , false);
  RightFrontDrive.rotateFor(distanceToTurns(degrees * robotCircumference / 360) , rev , true);
  if(robotStuck() == true){ //if robot is stuck then wait 0.25 seconds
    wait(0.25 , seconds);
    if(robotStuck() == true){ //if robot is still stuck, then stop drivetrain
      brakeDrivetrain();
    }
  }
  stopDrivetrain();
  wait(1 , seconds);
}

//======================================================================================================================== 


void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Screen.clearScreen();
  ballSorter.setLightPower(100 , pct);
  ballSorter.setLight(ledState::on);
  
  

  // 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 auton(void) {

  spinIntakesFwd();
  setDrivetrainSpeed(30);
  moveDrivetrainFor(16);
  turnDrivetrainFor(105);
  moveDrivetrainFor(24);
  spinMagazineFwd();
  wait (1 , seconds);
  stopManipulators();
  wait(0.5 , seconds);
  spinMagazineFwd();
  wait(0.5 , seconds);
  stopManipulators();
  wait(0.5 , seconds);
  spinMagazineFwd();
  stopManipulators();
  moveDrivetrainFor(-5);
  //vexcodeInit();
  // ..........................................................................
  // Insert autonomous user code here.
  
  // ..........................................................................
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {
  bool poopColor;
  //vexcodeInit();
  // User control code here, inside the loop
  if(ejectorSelector.angle(degrees) < 115){
      poopColor = true; //ejects red balls
      Brain.Screen.print("true");
    }
    
  if(ejectorSelector.angle(degrees) > 115){
    poopColor = false; //ejects blue balls
    Brain.Screen.print("false");
  }
  
  while (1) {
    // 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
    //moves drivetrain
    Brain.Screen.printAt(125 , 60 , "%f" , ejectorSelector.angle(degrees));
    //Brain.Screen.printAt(125 , 70 , "%f" , ballSorter.color());
    wait (1,seconds);
    Brain.Screen.clearScreen();
    if (poopColor == true){
      if(/*ballSorter.hue()  >0 && ballSorter.hue() < 15*/ballSorter.color() == red){
        if(ballSorter.isNearObject() == true){
          poop();
          wait(1 , seconds);
        }

      }
    }
    else if(poopColor == false){
      if(/*ballSorter.hue() > 180 && ballSorter.hue() < 240*/ ballSorter.color() == blue){
        if(ballSorter.isNearObject() == true){
          poop();
          wait(1 , seconds);
        } 
      }
    }
    while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
      spinDrivetrain();
      while(Controller1.ButtonR1.pressing()){
        spinManipulatorsFwd();
        spinDrivetrain();
      }
      while(Controller1.ButtonR2.pressing()){
        spinManipulatorsRev();
        spinDrivetrain();
      }
      while(Controller1.ButtonL1.pressing()){
        spinMagazineFwd();
        spinDrivetrain();
      }
      while(Controller1.ButtonL2.pressing()){
        spinMagazineRev();
        spinDrivetrain();
      }
      while (Controller1.ButtonX.pressing()){
        spinIntakesFwd();
        spinDrivetrain();
      }
      while (Controller1.ButtonB.pressing()){
        spinIntakesRev();
        spinDrivetrain();
      }
      stopManipulators();
    }//spins intakes and magazine forwards
    while(Controller1.ButtonR1.pressing()){
      spinManipulatorsFwd();
      while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
        spinDrivetrain();
      }
    }
    //spins intakes and magazine backwards
    while(Controller1.ButtonR2.pressing()){
      spinManipulatorsRev();
      while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
        spinDrivetrain();
      }
    }
    //spins magazine forwards
    while(Controller1.ButtonL1.pressing()){
      spinMagazineFwd();
    }
    //spins magazine backwards
    while(Controller1.ButtonL2.pressing()){
      spinMagazineRev();
    }
    //spins intakes forwards
    while (Controller1.ButtonX.pressing()){
      spinIntakesFwd();
      while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
        spinDrivetrain();
      }
    }
    while (Controller1.ButtonB.pressing()){
      spinIntakesRev();
      while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
        spinDrivetrain();
      }
    }
    while (Controller1.ButtonA.pressing()){
      poop();
      while(Controller1.Axis3.value() > 10 || Controller1.Axis3.value() < -10 || Controller1.Axis1.value() > 10 || Controller1.Axis1.value() < -10){
        spinDrivetrain();
      }
    }
    LeftFrontDrive.stop(coast);
    LeftRearDrive.stop(coast);
    RightFrontDrive.stop(coast);
    RightRearDrive.stop(coast);
    RightIntake.stop(coast);
    LeftIntake.stop(coast);
    TopMotor.stop(coast);
    BottomMotor.stop(coast);
    
    // ........................................................................
    // 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.
  

  // Run the pre-autonomous function.
  pre_auton();
  Competition.autonomous(auton);
  Competition.drivercontrol(usercontrol);
  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
/*
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  for{
    if(Controller1.Axis3 > 10){
      LeftFrontDrive.spin(fwd);
      LeftRearDrive.spin(fwd);
    }
  }
  
}
*/
}


1 Like

It sounds like your potentiometer is broken. Try a different potentiometer or a different sensor altogether and see if you notice a difference.

2 Likes

I tried a different potentiometer but ran I to the same issue

Try a different port.

1 Like

check the pot is working correctly using the V5 dashboard (devices screen, select 3wire on top line, then configure as an analog input)

3 Likes