Can't get multithreading to work

I have been trying to get the multithreading to work for AutoIntakeStarter, AutoIntakeStopper, and NoNegative. I already got CounterUP and CounterDOWN to work but I can’t figure out what is wrong with the other ones.

My Code

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here
int autonType = -1;
int Discs = 0;
double intakeRotate = (200 * 0.01666666666 * 3);
int timerSet = 0;
void CounterUP(void) {
  Discs = Discs + 1;
  Controller1.Screen.clearLine(3);
  Controller1.Screen.print(Discs);
  vex::task::sleep(1000);
}
void CounterDOWN(void) {
  Discs = Discs - 1;
  Controller1.Screen.clearLine(3);
  Controller1.Screen.print(Discs);
  vex::task::sleep(1000);
}
void AutoIntakeStarter(void) {
  timerSet = 3;
  Discs = 0;
}
void NoNegative(void) {
  Discs = 0;
}
void AutoIntakeStopper(void) {
  this_thread::sleep_for(1000);
  Brain.Screen.clearLine(5);
  Brain.Screen.clearLine(6);
  Brain.Screen.printAt(1, 100, "Out-taking");
  Controller1.Screen.clearLine(3);
  Controller1.Screen.print(Discs);
  Intake.startSpinFor(reverse, intakeRotate, rev, 600, rpm);
  this_thread::sleep_for(3000);
  timerSet = timerSet - 3;
  Brain.Screen.clearLine(5);
  Brain.Screen.clearLine(6);
  Intake.setRotation(0, rev);
}

/*---------------------------------------------------------------------------*/
/*                          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();
  bool autonSelecterStatus = false;
  ColorSense1.setLight(ledState::on);
  ColorSense1.gestureEnable();
  Brain.Screen.printAt(1, 40, "Auton");
  while (true) {
    if (autonSelecter.value() > 0 && autonSelecterStatus == false) {
      autonSelecterStatus = true;
    } else if (autonSelecter.value() == 0 && autonSelecterStatus == true) {
      Brain.Screen.clearScreen();
      if (autonType == 0 || autonType == -1) {
        autonType = 1;
        Brain.Screen.printAt(1, 40, "1st Auton");
      }else if (autonType == 1) {
        autonType = 2;
        Brain.Screen.printAt(1, 40, "2nd Auton");
      }else if (autonType == 2) {
        autonType = 3;
        Brain.Screen.printAt(1, 40, "3rd Auton");
      }else if (autonType == 3) {
        autonType = 4;
        Brain.Screen.printAt(1, 40, "4th Auton");
      }else if (autonType == 4) {
        autonType = 5;
        Brain.Screen.printAt(1, 40, "5th Auton");
      }else if (autonType == 5) {
        autonType = 0;
        Brain.Screen.printAt(1, 40, "Do Nothing");
      }
      Controller1.Screen.print(autonType);
      autonSelecterStatus = false;
    }
    wait (20, msec);
  }


  // 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) {
/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if(autonType == 0) {
    wait(10, msec);
  }
  if (autonType == 1) {
    L1.spin(fwd, 10, percent);
    R1.spin(fwd, 10, percent);
    L2.spin(fwd, 10, percent);
    R2.spin(fwd, 10, percent);
  wait(5, sec);
    L1.stop(brake);
    L2.stop(brake);
    R1.stop(brake);
    R2.stop(brake);
  }

/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 2) {
    wait(10, msec);
    
  }


/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 3) {
    
  }


/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 4) {
    
  }



/*---------------------------------------------------------------------------*/
/*                          Fake Auto if we need it                          */
/*---------------------------------------------------------------------------*/

  if (autonType == 5) {
    
  }
}

/*---------------------------------------------------------------------------*/
/*                              End of Auton's                               */
/*---------------------------------------------------------------------------*/



/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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 brakeSystem = false;
  bool shootSystem = false;
  bool intakeSystem = false;
  ColorSense1.setLight(ledState::on);
  ColorSense1.setLightPower(100);
  ColorSense1.gestureEnable();
  FWSM1.setRotation(0, rev);
  FWSM2.setRotation(0, rev);
  Intake.setRotation(0, rev);
  Brake.setRotation(0, rev);
  while (1) {
      //Driver Controls for Mechanum Wheels.
      if(Controller1.Axis3.position() > -15 && Controller1.Axis3.position() < 15 && Controller1.Axis1.position() > -15 && Controller1.Axis1.position() < 15 && Controller1.Axis4.position() > -15 && Controller1.Axis4.position() < 15) {
      Brain.Screen.clearLine(10);
      Brain.Screen.clearLine(11);
      L1.stop(brake);
      L2.stop(brake);
      R1.stop(brake);
      R2.stop(brake);
      Brain.Screen.printAt(1, 200, "Not Moving");
      }else {
      Brain.Screen.clearLine(10);
      Brain.Screen.clearLine(11);
      L1.spin(fwd, Controller1.Axis3.position() + Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
      R1.spin(fwd, Controller1.Axis3.position() - Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
      L2.spin(fwd, Controller1.Axis3.position() + Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
      R2.spin(fwd, Controller1.Axis3.position() - Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
      Brain.Screen.printAt(1, 200, "Moving");
      }
  

      //Shooting system
      if(Controller1.ButtonR1.pressing() && shootSystem == false) {
      shootSystem = true;
      } else if (!Controller1.ButtonR1.pressing() && shootSystem == true) {
        if(FWSM1.rotation(rev) < .5 && FWSM2.rotation(rev) < .5) {
        FWSM1.spin(fwd, 100, pct);
        FWSM2.spin(reverse, 100, pct);
        Brain.Screen.printAt(1, 70, "Shooting");
        }else {
        FWSM1.stop(brake);
        FWSM2.stop(brake);
        FWSM1.setRotation(0, rev);
        FWSM2.setRotation(0, rev);
        Brain.Screen.clearLine(3);
        Brain.Screen.clearLine(4);
        }
        shootSystem = false;
      }

      //Intake System  

      //Color Sensor stopping the Intake after 3 Discs
/*      if (Discs == 3) {
        timerSet = 3;
        Discs = 0;
      }

      if(Discs < 0) {
        Discs = 0;
      }

      if (timerSet != 0) {
        while(timerSet != 0) {
          this_thread::sleep_for(1000);
          Brain.Screen.clearLine(5);
          Brain.Screen.clearLine(6);
          Brain.Screen.printAt(1, 100, "Out-taking");
          Controller1.Screen.clearLine(3);
          Controller1.Screen.print(Discs);
          Intake.startSpinFor(reverse, intakeRotate, rev, 600, rpm);
          this_thread::sleep_for(3000);
          timerSet = timerSet - 3;
          Intake.setRotation(0, rev);
        } 
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        Intake.setRotation(0, rev);
      }
      */
      //Manual Start-Stop of the Intake System
      if (Controller1.ButtonR2.pressing() && intakeSystem == false) {
      intakeSystem = true;
      } else if (!Controller1.ButtonR2.pressing() && intakeSystem == true) {
        if (Intake.rotation(rev) < .5) {
        Intake.spin(fwd, 100, pct);
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        Brain.Screen.printAt(1, 100, "Intaking");  
        } else {
        Intake.stop(brake);
        Intake.setRotation(0, rev);
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        }
        intakeSystem = false;
      } 

      


      //Brake System
      if(Controller1.ButtonL2.pressing() == true && brakeSystem == false) {
        brakeSystem = true;
      }
      else if (!Controller1.ButtonL2.pressing() && brakeSystem == true) {
        if(Brake.rotation(rev) < .3 && Brake.rotation(rev) > -100) {
          Brain.Screen.clearLine(7);  
          Brain.Screen.printAt(1, 140, "Brake Down");
          Brake.startSpinFor(fwd, 1.2, rev, 200, rpm);
        }else {
          Brain.Screen.clearLine(7);
          Brain.Screen.printAt(1, 140, "Brake Up");
          Brake.startSpinFor(reverse, 1.2, rev, 200, rpm);
          Brake.setRotation(0, rev);
        }
        brakeSystem = false;
        Brake.setRotation(0, rev);
      }


    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 and for when the color sensor counts the incoming discs.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  ColorSense1.gestureUp(CounterUP);
  ColorSense1.gestureDown(CounterDOWN);
  if (Discs == 3) {
  AutoIntakeStarter();
  } if(Discs < 0) {
  NoNegative();
  } if (timerSet != 0) {
  AutoIntakeStopper();
  }
  // Run the pre-autonomous function.
  pre_auton();

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

Your functions CounterUP and CounterDOWN work because they are registered as “event handlers”. Behind the scenes, when you register functions as event handlers like this, the ColorSense1 object is, in a sense, abstracting the thread creation and management process for you.

  ColorSense1.gestureUp(CounterUP);
  ColorSense1.gestureDown(CounterDOWN);

Your functions AutoIntakeStarter, NoNegative, and AutoIntakeStopper are called once, in main, before the while loop happens. Without fully reviewing your code, I would suspect the conditions are not true at the start of the program, and these functions do not execute because of the if logic.

To create a thread to run AutoIntakeStarter, for example, you would do something like:

  thread *autoIntakeStarterThread = new thread(AutoIntakeStarter);

You would likely want to move this back into your usercontrol while loop, inside your if (Discs == 3) { condition.

That said, based on your apparent experience level, I would not recommend doing it this way, as you may wind up inadvertently creating many thread instances executing this function.

You likely don’t need to create threads for AutoIntakeStarter and NoNegative. You can probably move them (and the conditionals around them) back to userControl.

You may then need to change AutoIntakeStopper to run in a loop, while running in a separate thread. So:

void AutoIntakeStopper(void) {
  while(1) {
    if (timerSet != 0) {
      this_thread::sleep_for(1000);
      Brain.Screen.clearLine(5);
      Brain.Screen.clearLine(6);
      Brain.Screen.printAt(1, 100, "Out-taking");
      Controller1.Screen.clearLine(3);
      Controller1.Screen.print(Discs);
      Intake.startSpinFor(reverse, intakeRotate, rev, 600, rpm);
      this_thread::sleep_for(3000);
      timerSet = timerSet - 3;
      Brain.Screen.clearLine(5);
      Brain.Screen.clearLine(6);
      Intake.setRotation(0, rev);
    }
  }
}

Your usercontrol function will now need to start like:

void usercontrol(void) {
  bool brakeSystem = false;
  bool shootSystem = false;
  bool intakeSystem = false;
  ColorSense1.setLight(ledState::on);
  ColorSense1.setLightPower(100);
  ColorSense1.gestureEnable();
  FWSM1.setRotation(0, rev);
  FWSM2.setRotation(0, rev);
  Intake.setRotation(0, rev);
  Brake.setRotation(0, rev);
  // Start a thread to run AutoIntakeStarter
  thread *autoIntakeStarterThread = new thread(AutoIntakeStarter);
  while (1) {
  // ... rest of your code here

In main before your while(1) loop there you’ll probably want to explicitly set:

  Competition.bStopAllTasksBetweenModes = true;

So that during a competition if your robot exits usercontrol and then restarts usercontrol only one instance of this thread is running at a time.

5 Likes

Thank you so much for your explanation. I understand much better now. The only question I have is after I set all of that up its giving me the error message when I put the new thread above the while loop of usercontrol

Unused variable ‘AutoIntakeStopperThread’

That’s the only thing in not sure about.

[Edit] I also tried putting AutoIntakeStopperThread into the if (Discs == 3) condition but that didn’t work either

      if (Discs == 3) {
        AutoIntakeStopperThread;
        timerSet = 3;
        Discs = 0;
      }

It gave me an error of Expression result unused

Post your full code again, please?

2 Likes

This code gives me the following error: ’

Unused variable ‘AutoIntakeStopperThread’.

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here
int autonType = -1;
int Discs = 0;
double intakeRotate = (200 * 0.01666666666 * 3);
int timerSet = 0;
void CounterUP(void) {
  Discs = Discs + 1;
  Controller1.Screen.clearLine(3);
  Controller1.Screen.print(Discs);
  vex::task::sleep(1000);
}
void CounterDOWN(void) {
  Discs = Discs - 1;
  Controller1.Screen.clearLine(3);
  Controller1.Screen.print(Discs);
  vex::task::sleep(1000);
}
void AutoIntakeStopper(void) {
  while (1) {
    if (timerSet != 0) {
      this_thread::sleep_for(1000);
      Brain.Screen.clearLine(5);
      Brain.Screen.clearLine(6);
      Brain.Screen.printAt(1, 100, "Out-taking");
      Controller1.Screen.clearLine(3);
      Controller1.Screen.print(Discs);
      Intake.startSpinFor(reverse, intakeRotate, rev, 600, rpm);
      this_thread::sleep_for(3000);
      timerSet = timerSet - 3;
      Brain.Screen.clearLine(5);
      Brain.Screen.clearLine(6);
      Intake.setRotation(0, rev);
    }
  }
}
/*---------------------------------------------------------------------------*/
/*                          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();
  bool autonSelecterStatus = false;
  ColorSense1.setLight(ledState::on);
  ColorSense1.gestureEnable();
  Brain.Screen.printAt(1, 40, "Auton");
  while (true) {
    if (autonSelecter.value() > 0 && autonSelecterStatus == false) {
      autonSelecterStatus = true;
    } else if (autonSelecter.value() == 0 && autonSelecterStatus == true) {
      Brain.Screen.clearScreen();
      if (autonType == 0 || autonType == -1) {
        autonType = 1;
        Brain.Screen.printAt(1, 40, "1st Auton");
      }else if (autonType == 1) {
        autonType = 2;
        Brain.Screen.printAt(1, 40, "2nd Auton");
      }else if (autonType == 2) {
        autonType = 3;
        Brain.Screen.printAt(1, 40, "3rd Auton");
      }else if (autonType == 3) {
        autonType = 4;
        Brain.Screen.printAt(1, 40, "4th Auton");
      }else if (autonType == 4) {
        autonType = 5;
        Brain.Screen.printAt(1, 40, "5th Auton");
      }else if (autonType == 5) {
        autonType = 0;
        Brain.Screen.printAt(1, 40, "Do Nothing");
      }
      Controller1.Screen.print(autonType);
      autonSelecterStatus = false;
    }
    wait (20, msec);
  }


  // 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) {
/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if(autonType == 0) {
    wait(10, msec);
  }
  if (autonType == 1) {
    L1.spin(fwd, 10, percent);
    R1.spin(fwd, 10, percent);
    L2.spin(fwd, 10, percent);
    R2.spin(fwd, 10, percent);
  wait(5, sec);
    L1.stop(brake);
    L2.stop(brake);
    R1.stop(brake);
    R2.stop(brake);
  }

/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 2) {
    wait(10, msec);
    
  }


/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 3) {
    
  }


/*---------------------------------------------------------------------------*/
/*                              Description                                  */
/*---------------------------------------------------------------------------*/

  if (autonType == 4) {
    
  }



/*---------------------------------------------------------------------------*/
/*                          Fake Auto if we need it                          */
/*---------------------------------------------------------------------------*/

  if (autonType == 5) {
    
  }
}

/*---------------------------------------------------------------------------*/
/*                              End of Auton's                               */
/*---------------------------------------------------------------------------*/



/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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 brakeSystem = false;
  bool shootSystem = false;
  bool intakeSystem = false;
  ColorSense1.setLight(ledState::on);
  ColorSense1.setLightPower(100);
  ColorSense1.gestureEnable();
  FWSM1.setRotation(0, rev);
  FWSM2.setRotation(0, rev);
  Intake.setRotation(0, rev);
  Brake.setRotation(0, rev);
  // Start a thread to run AutoIntakeStopper
  thread *AutoIntakeStopperThread = new thread(AutoIntakeStopper);
  while (1) {
      //Driver Controls for Mechanum Wheels.
      if(Controller1.Axis3.position() > -15 && Controller1.Axis3.position() < 15 && Controller1.Axis1.position() > -15 && Controller1.Axis1.position() < 15 && Controller1.Axis4.position() > -15 && Controller1.Axis4.position() < 15) {
      Brain.Screen.clearLine(10);
      Brain.Screen.clearLine(11);
      L1.stop(brake);
      L2.stop(brake);
      R1.stop(brake);
      R2.stop(brake);
      Brain.Screen.printAt(1, 200, "Not Moving");
      }else {
      Brain.Screen.clearLine(10);
      Brain.Screen.clearLine(11);
      L1.spin(fwd, Controller1.Axis3.position() + Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
      R1.spin(fwd, Controller1.Axis3.position() - Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
      L2.spin(fwd, Controller1.Axis3.position() + Controller1.Axis1.position() - Controller1.Axis4.position(), percent);
      R2.spin(fwd, Controller1.Axis3.position() - Controller1.Axis1.position() + Controller1.Axis4.position(), percent);
      Brain.Screen.printAt(1, 200, "Moving");
      }
  

      //Shooting system
      if(Controller1.ButtonR1.pressing() && shootSystem == false) {
      shootSystem = true;
      } else if (!Controller1.ButtonR1.pressing() && shootSystem == true) {
        if(FWSM1.rotation(rev) < .5 && FWSM2.rotation(rev) < .5) {
        FWSM1.spin(fwd, 100, pct);
        FWSM2.spin(reverse, 100, pct);
        Brain.Screen.printAt(1, 70, "Shooting");
        }else {
        FWSM1.stop(brake);
        FWSM2.stop(brake);
        FWSM1.setRotation(0, rev);
        FWSM2.setRotation(0, rev);
        Brain.Screen.clearLine(3);
        Brain.Screen.clearLine(4);
        }
        shootSystem = false;
      }

      //Intake System  

      //Color Sensor stopping the Intake after 3 Discs
      if (Discs == 3) {
        timerSet = 3;
        Discs = 0;
      }

      if(Discs < 0) {
        Discs = 0;
      }
/*
      if (timerSet != 0) {
        while(timerSet != 0) {
          this_thread::sleep_for(1000);
          Brain.Screen.clearLine(5);
          Brain.Screen.clearLine(6);
          Brain.Screen.printAt(1, 100, "Out-taking");
          Controller1.Screen.clearLine(3);
          Controller1.Screen.print(Discs);
          Intake.startSpinFor(reverse, intakeRotate, rev, 600, rpm);
          this_thread::sleep_for(3000);
          timerSet = timerSet - 3;
          Intake.setRotation(0, rev);
        } 
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        Intake.setRotation(0, rev);
      }
      */
      //Manual Start-Stop of the Intake System
      if (Controller1.ButtonR2.pressing() && intakeSystem == false) {
      intakeSystem = true;
      } else if (!Controller1.ButtonR2.pressing() && intakeSystem == true) {
        if (Intake.rotation(rev) < .5) {
        Intake.spin(fwd, 100, pct);
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        Brain.Screen.printAt(1, 100, "Intaking");  
        } else {
        Intake.stop(brake);
        Intake.setRotation(0, rev);
        Brain.Screen.clearLine(5);
        Brain.Screen.clearLine(6);
        }
        intakeSystem = false;
      } 

      


      //Brake System
      if(Controller1.ButtonL2.pressing() == true && brakeSystem == false) {
        brakeSystem = true;
      }
      else if (!Controller1.ButtonL2.pressing() && brakeSystem == true) {
        if(Brake.rotation(rev) < .3 && Brake.rotation(rev) > -100) {
          Brain.Screen.clearLine(7);  
          Brain.Screen.printAt(1, 140, "Brake Down");
          Brake.startSpinFor(fwd, 1.2, rev, 200, rpm);
        }else {
          Brain.Screen.clearLine(7);
          Brain.Screen.printAt(1, 140, "Brake Up");
          Brake.startSpinFor(reverse, 1.2, rev, 200, rpm);
          Brake.setRotation(0, rev);
        }
        brakeSystem = false;
        Brake.setRotation(0, rev);
      }


    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 and for when the color sensor counts the incoming discs.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  ColorSense1.gestureUp(CounterUP);
  ColorSense1.gestureDown(CounterDOWN);
  Competition.bStopAllTasksBetweenModes = true;
  // Run the pre-autonomous function.
  pre_auton();

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

Unused variable and unused expression are warnings not errors, warnings mean that your code will still compile but the compiler is basically telling you that you might have made a mistake when you wrote your program. In this case, these warnings can both be ignored, and you can compile and download your code.

4 Likes

Its compiles but it doesn’t run correctly.

Well that’s different from the warnings. What do you want the program to do, and what does it actually do?

Well when I just put the thread *AutoIntakeStopperThread = new thread(AutoIntakeStopper); line in the void usercontrol(void) { it gives me the Unused variable ‘AutoIntakeStopperThread’ warning error, but its not being called anywhere so I tried putting thread *AutoIntakeStopperThread = new thread(AutoIntakeStopper); it in the if (Discs == 3) { and it gave the same warning. Lastly I tried putting just AutoIntakeStopperThread in the if(Discs == 3) { like this

      if (Discs == 3) {
        AutoIntakeStopperThread;
        timerSet = 3;
        Discs = 0;
      }

but then it gives me the warning error Expression result unused

yea, don’t use “new”, no need to allocate memory for that thread. use

  thread AutoIntakeStopperThread = thread(AutoIntakeStopper);

which will probably clear the warning.
anytime you have unused variable, the easiest way to squash the warning (without using the variable) is to just use void, as in.

(void)AutoIntakeStopperThread;
5 Likes

I got it working by taking out the thread *AutoIntakeStopperThread = new thread(AutoIntakeStopper); and putting vex::thread AutoIntakeStopperThread(AutoIntakeStopper); in the if (Discs == 3) { like this:

      if (Discs == 3) {
        timerSet = 3;
        vex::thread AutoIntakeStopperThread(AutoIntakeStopper);
        Discs = 0;
      }

That got it working so thank all of you guys for your help. I understand multithreading pretty well now.