My team members really don’t know much about coding, they are slowly learning. We are using Vex C++ in Vex Coding Studio. As part of our driver control program, we have one motor to change the angle of the launcher. If it goes to 220 degrees that is perfect for the lower flags, and 380 is perfect for the higher flags. However, if it goes much past 400 it is too far, and it cannot rotate back. We can use that successfully in our autonomous program, making it go from loading the ball at zero and then going to the desired point and shooting the ball. But we have been trying many things to make it useful in the driver control. We would like to have a single button on the controller take it to the low flag shooting height, and another button take it to the higher shooting height, and also put a limit on the motor so it will never go past 400. Below is the code we are using now, just a simple if / else statement. Can anyone offer us any insight into coding what we want the motor to do?
//LaunchLifter Control
if(Controller2.ButtonX.pressing()) {
//If the button X is pressed this Spins the LaunchLifter motor forward to raise the Launcher.
LaunchLifter.spin(vex::directionType::fwd, LaunchLifterPCT, vex::velocityUnits::pct);
}
else if(Controller2.ButtonB.pressing()) {
//If the button B is pressed this Spins the LaunchLifter motor backward to lower the Launcher.
LaunchLifter.spin(vex::directionType::rev, LaunchLifterPCT, vex::velocityUnits::pct);
}
else {
//If the X or B buttons are not pressed the LaunchLifter motor is stopped.
LaunchLifter.stop(vex::brakeType::brake);
}
Yes, the boys used rotateTo in the autonomous program, it works great. But what we cannot figure out is how to tie it to a button on the controller in the driver command program, and then tie it to another button for a different number of degrees. They’ve tried several things; there seems to be conflicts in using both buttons to control the same motor.
I think the boys had used that, making an if statement where button left goes to 220 and another where button right goes to 380, but they couldn’t figure out what to put in for else… they couldn’t leave it blank. If they made the motors stop if the left button is not pushed, that contradicts the motor moving if the right button pushed. What should go in for the else part of the statement?
If they use startRotateTo using the 220 target for one button and the 380 for the other (as @John_TYler suggested,) the motor would stop on its own once those targets are reached. You would not need an else to stop the motors.
Really? They wouldn’t need if…else?? OK, I’ll tell them and have them try that tomorrow. I think everything they were doing was either in a loop or if…else statements. Thank you.
You’d need an else statement if the motor was being set to a speed, or given another command that told it do something indefinitely.
If you send a motor.spin() command, the motor will start spinning and not stop or change until it gets another command (or the program execution ends). So for that case you need a way to tell the motor to stop moving if the button isn’t being pressed. That’s the else statement you have above.
The startRotateTo() command tells the motor to move until it gets to the encoder count you send it, then stop. No else statement necessary.
Note the difference, rotateTo() gives the motor the command and waits for the motor to complete before it moves to the next line in your code (blocking). startRotateTo() sends the command and immediately allows your code to continue execution while the motor does its thing.
What I did was just have the rotateTo for the first angle set to one button with an if statement, have a rotateTo for the second angle set to another button with an else if statemnt, then have the else be a hold for the motor, even though this is the default command after using rotateTo, and it works perfectly