If statement help. Making things run as I want them to!

int main() {
On.rotateFor(5.35,timeUnits::sec,15,velocityUnits::pct);
On.stop();
if(On.stop();){
Main.rotateFor(5,timeUnits::sec,30,velocityUnits::pct);
Off.spin(directionType::fwd,30,velocityUnits::rpm);
}
else{
Main.stop();
Off.stop();
}
}

This is my code. What I want to happen is, I want the “Main” motor to start running whenever the “On” motor stops. I am looking for what I should put in the ( ) of my if statement. Any help would be greatly appreciated!

@Nathan1, you don’t really need if statements here, because rotateFor() function could accept an additional argument, which tells it to wait for the rotation completion:

rotateFor(double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion=true)

If you know that nothing would prevent “on” motor to reach the target position, then your code could look like this:

int main()
{
   On.rotateFor(5.35,timeUnits::sec,15,velocityUnits::pct, true); 
   Main.rotateFor(5,timeUnits::sec,30,velocityUnits::pct,true);
   Off.spin(directionType::fwd,30,velocityUnits::rpm);
}

However, if there is a chance that “On” motor could run into something then you will need code like this:

int main()
{
   On.rotateFor(5.35,timeUnits::sec,15,velocityUnits::pct, false); 
   while(On.isSpinning) { vex::task::sleep(5); }
   Main.rotateFor(5,timeUnits::sec,30,velocityUnits::pct,false);
   while(Main.isSpinning) { vex::task::sleep(5); }
   Off.spin(directionType::fwd,30,velocityUnits::rpm);
   vex::task::sleep(2000); // let it run for 2 seconds
   Off.stop();
}

The reference pages for Task.sleep() and Motor.isSpinning() functions:

https://help.vexcodingstudio.com/#pro/namespacevex/classvex_1_1task/sleep
https://help.vexcodingstudio.com/#cpp/namespacevex/classvex_1_1motor/isSpinning

Also, next time you may want to wrap your code into BBCode [code]...[/code] tags for better readability (you can even edit your post and add them now).

2 Likes

Guess I was just thinking about it too much. Thanks for the help!