UNOFFICIAL: Conveyor belt programming

In this thread, @floop1459 asked why their conveyor belt was stuttering significantly in one direction, but not the other, using this code:


if (vexRT[Btn7U] == 1)
{
motor[WI] = -127;
motor[WI1] = -127;
}
else if (vexRT[Btn7U] == 0)
{
motor[WI] = 0;
motor[WI1] = 0;
}

if (vexRT[Btn5U] == 1)
{
motor[WI1] = 127;
motor[WI] = 127;
}
else if (vexRT[Btn5U] == 0)
{
motor[WI1] = 0;
motor[WI] = 0;
}

I don’t know why the motors were not stuttering in both directions. The problem is that set the motors to 2 different powers in each loop if either button is pressed (if neither are pressed, the motors are set to 0 power twice). There are 2 ways to solve this problem, depending on what you are trying to do.
If this is a teaching exercise to show if and else if statements, you can do this:


if(vexRT[Btn7U] == 1) {
  motor[WI] = -127;
  motor[WI1] = -127;
}
else if(vexRT[5U] == 1) {
  motor[WI] = 127;
  motor[WI1] = 127;
}
else {
  motor[WI] = 0;
  motor[WI1] = 0;
}

This will check to see if button 7 Up is pressed. If it is, the motors spin backwards at full power. If it is not, it checks if button 5 Up is pressed. If it is, the motors spin forward at full power. If neither button is pressed, the motors are shut off. This gives button 7 Up priority, so if you want the motors to spin backwards if both buttons are pressed, do this. If you want them to spin forwards if both are pressed, switch the button 5 Up and button 7 Up blocks, making the 5U block an if statement and the 7U an else if statement.

Also, you can replace


motor[WI] = 0;
motor[WI1] = 0;

(Or any other power level)

with


motor[WI] =
  motor[WI1] =
  0;

This is useful when you use math to figure out the power level.

This is the other method:


motor[WI] =
  motor[WI1] =
  127 * (vexRT[Btn5U] - vexRT[Btn7U]);

This makes power positive if button 5U is pressed and 7U isn’t; power is negative if 7U is pressed and 5U isn’t, and if both or neither is pressed, power is 0. This has the benefit of being really compact (3 lines instead of 12, and still clean) and the robot does nothing if your driver accidentally pressed both buttons, which prevents unwanted behavior. My team uses the second method, but many teams use the first. It is your preference, and really doesn’t matter much in the long run. If anyone has more advice, thoughts on my sample code, alternative methods, or any other input, feel free to contribute.