Help please. nonblocking function

Hi Vex forum. I am asking for your help. I have tried searching the forum and did not find a solution yet. I use Vexcode pro.

Here is what I want to achieve,

  1. low the backlift/fork to the ready position (potentiometer angle =36 degree)
  2. the drive train moves backward (to the neutral zone)
  3. lift the mobile goal with the backlift
  4. the drive train moves forward (to the home zone)

Here is my current code. However, step 2 will wait until step 1 is finished. I wonder if there is a way to run step 1 and 2 simultaneously.

Thank you very much for your help

void autonomous(void) {
      waitUntil(Potentiometer.angle( degrees )<36);
      Drivetrain.driveFor(reverse, 62, inches);
      waitUntil(Potentiometer.angle( degrees )>90);
     Drivetrain.driveFor(forward, 62, inches);

you use the command waitUntil which is could only assume “waits until the potentiometer angle is less than 36 degrees” to move onto the next step. The easiest way to fix this would be to make a function that uses an if statement to determine whether or not the angle of the potentiometer is less than your specified degrees.

1 Like

Could you elaborate more? I try the following and it does not work.

void goready() {
if (Potentiometer.angle( degrees )>36) {
 } else backlift.stop(hold);

Assuming your potentiometer is attached to the same axle as your backlift (and if it’s not, simply apply the gear ratio between the two to the following):

void autonomous(void) {
  double motorToPotentiometerGearRatio = 1.0;  // Or whatever the case is
  double initialPotentiometerReading = Potentiometer.angle(degrees);
  double desiredEndPotentiometer = 36;
  // Calculate how much you want the motor to rotate
  double desiredBackLiftDeployment = motorToPotentiometerGearRatio * ( desiredEndPotentiometer -  initialPotentiometerReading );
   double backliftDeploymentSpeed = 100;

   // The last parameter here is set to false so the motor will spin without waiting for its movement to complete. This is the way to use the non-blocking feature
   backlift.spinFor(reverse, desiredBackLiftDeployment , degrees, 100, percent, false);
   Drivetrain.driveFor(reverse, 62, inches);
   // Do similar to lift the mobile goal

Thank you very much for the detailed code and clever suggestion. This is essentially similar to use the motor encode to rotate to the desired angle and set waitforcompletion to false.

The reason I want to use the potentiometer instead of the motor encoder is that the staring point of back lift may not always the same and use absolute position/angle of potentiometer is more accurate.

I will definitely try this. However, I wonder if there is other way with potentiometer only.

You can do this with threads. You can have a thread running the waitUntil so that the motor will still stop with the potentiometer. This way, you can move on to other things while that runs in the background.


I think you are partly on the right track, but not fully there. Your idea to use the potentiometer is good, because it can retain its state through the robot turning off/on, etc. It should tolerate if you set up the backlift at slightly different angles each match. This is fantastic and well-thought out.

However, you seem fixated on exclusively using the potentiometer to solve the problem you’ve identified. The sensor tells you where you are, and you know where you want to go. Telling the motor to move that amount should be functionally equivalent to waiting for the sensor to reach a value.

As @xTigr mentions, there are other ways to solve this problem, such as using threads. IMO, this introduces unnecessary complexity, and may make debugging and tuning more difficult.


Thank you all. I will try both and it is really a great learning experience.