How to make two loops running at the same time?

What is wrong with our current drive code? We are trying to have our robot tank control and be able to move our robot arm up and down as well as open and close our claw. So far, we have been able to fix the drive issue but as soon as we did, the claw and lift arm stopped working, any ideas on how to fix our code? Do we need to make two loops?

void usercontrol( void ) {
  // User control code here, inside the loop
   
  while(true) {
        FrontLeftDrive.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
        BackLeftDrive.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
        FrontRightDrive.spin(vex::directionType::rev, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis3-Axis4)/2
        BackRightDrive.spin(vex::directionType::rev, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis3-Axis4)/2
  }
  

    //Use these variables to set the speed of the arm and claw.
    int armSpeedPCT = 50;
    int clawSpeedPCT = 50;
    
        if(Controller1.ButtonUp.pressing()) {
            LeftLift.spin(directionType::fwd, armSpeedPCT, velocityUnits::pct);
            RightLift.spin(directionType::rev, armSpeedPCT, velocityUnits::pct);
        }
        else if(Controller1.ButtonDown.pressing()) {
            LeftLift.spin(directionType::rev, armSpeedPCT, velocityUnits::pct);
            RightLift.spin(directionType::fwd, armSpeedPCT, velocityUnits::pct);
        }
        else {
            LeftLift.stop(brakeType::brake);
            RightLift.stop(brakeType::brake);
        }

        if(Controller1.ButtonA.pressing()) {
            LeftClaw.spin(directionType::fwd, clawSpeedPCT, velocityUnits::pct);
            RightClaw.spin(directionType::rev, clawSpeedPCT, velocityUnits::pct);
        }
        else if(Controller1.ButtonY.pressing()) {
            LeftClaw.spin(directionType::rev, clawSpeedPCT, velocityUnits::pct);
            RightClaw.spin(directionType::fwd, clawSpeedPCT, velocityUnits::pct);
        }
        else {
            LeftClaw.stop(brakeType::brake);
            RightClaw.stop(brakeType::brake);
        }

        task::sleep(20);
    }

Try putting the code to lift and arm controls inside the drive while loop.

I’m guessing that the lift controls don’t work because that while loop will never end.

All you operator control stuff will normally always go into the mainloop (except for variable declarations).

You can use “Tasks” if you want (as it’s called in PROS) to run multiple things simultaneously but I think it’s unnecessary in this case.

1 Like

Use cases. A quick search will probably have some good results. I also made a thread much earlier about how to code macros which also has good info on it. There’s also something called Async Controllers which I think are related but don’t take my word for it. If someone can help with that, please teach me what they are.

I think it’s because the code runs top to bottom, and if you’re driving, then the code gets stuck at the drive loop and doesn’t continue.
Maybe try putting your arm code inside the drive loop

1 Like

For teleop, all of your controls must be inside the forever/while(true) loop (or at the least the control portion should be if you want to break into sub functions, which sub functions are called but not required to be inside a loop).

In your code, only the drivetrain commands are in that loop. Your arm control is outside of the loop. And because you have a forever loop, your code will never exit the loop and ever get to your arm and claw code. That is the issue.

So, just put all of your if/else conditions for the arm/claw inside the loop. make sure your armSpeedPCT and clawSpeedPCT variables are declared before the while(true) loop, otherwise you will be declaring new instances of these variables every few milliseconds and may overload your brain’s memory.

I don’t know what the command setting the task sleep to 20ms does… maybe it should also be left out of the loop.

1 Like