When the second button in my if else statement is pressing the motor stutters and barley moves. I believe it is a priority problem. Has anyone encountered and fixed this problem. This is the code:
if(Controller1.ButtonL2.pressing() || Controller1.ButtonL1.pressing()){
//spins other direction
conveyor.setVelocity(80,percent);
if(Controller1.ButtonL2.pressing()){
conveyor.spin(forward);
}
else{
conveyor.spin(reverse);
}
}
else{
// stops the conveyor from spinning
conveyor.stop(hold);
conveyor.setVelocity(0, percent);
}
I believe the issue is caused by your else statement. In the second if statement, you tell the robot to spin the conveyor in reverse if nothing is pressed. Then you include an else loop below your two if statements telling it to stop if nothing is pressed. If it’s in a while loop, the robot checks your program top to bottom, so it would spin in reverse for some time and stop. The while loop keeps repeating this, causing the stutter. If I’m interpreting what you want the robot to do correctly, you should remove one of the else statements or make it to where they both can’t run when the robot does its run through of the code.
I rue the day that others convinced me that separating “set motor velocity” from “spin the motor” was a good idea. It came from the concepts used in Modkit and then transitioned into VEX coding studio. It’s somewhat appropriate for blocks but, in my opinion anyway, is a terrible idea for text. It was made worse when it was decided that if a motor was already moving (ie. it had received a spin command) it would change the existing velocity, another spin command wasn’t needed. The problem is that the command is “setVelocity” and velocity has both direction and speed involved, so if the motor has been asked to “spin(reverse)” (having direction involved in motor spin commands is yet another terrible idea IMHO) and then you say “setVelocity(80, percent)” the motor thinks it is being asked to run at 80 percent speed forwards. That’s the root cause of the motor chattering, it gets told to go forwards at 80% and then reverse.
There are a number of ways to solve this, one would be to move this default 80% speed outside of any loops. You can also set the stopping mode there.
int main() {
conveyor.setVelocity(80,percent);
conveyor.setStopping(hold);
while(1) {
if(Controller1.ButtonL2.pressing() || Controller1.ButtonL1.pressing()){
//spins other direction
if(Controller1.ButtonL2.pressing()){
conveyor.spin(forward);
}
else{
conveyor.spin(reverse);
}
}
else{
// stops the conveyor from spinning
conveyor.stop(hold);
}
this_thread::sleep_for(20);
}
}
The alternative, which is my preference as it makes reading/understanding what the code does easier as the setVelocity call may be many lines of code away, is to use spin commands that include speed.
int main() {
conveyor.setStopping(hold);
while(1) {
if(Controller1.ButtonL2.pressing() || Controller1.ButtonL1.pressing()){
//spins other direction
if(Controller1.ButtonL2.pressing()){
conveyor.spin(forward, 80, percent);
}
else{
conveyor.spin(reverse, 80, percent);
}
}
else{
// stops the conveyor from spinning
conveyor.stop(hold);
}
this_thread::sleep_for(20);
}
}