How does this code work?

I am one of the programmers on my team, and somebody else on the team made this piece of code to control a shooting mechanism that works using six different motors.


if(vexRT[Btn7D] == 1)
{
	motor[left_bttm] = 45;
	motor[left_mid] = 45;
	motor[left_top] = 45;
	motor[right_bttm] = 45;
	motor[right_mid] = 45;
	motor[right_top] = 45;
	wait1Msec(100);
}
else
{
	motor[left_bttm] = 0;
	motor[left_mid] = 0;
	motor[left_top] = 0;
	motor[right_bttm] = 0;
	motor[right_mid] = 0;
	motor[right_top] = 0;
}

if(vexRT[Btn7L] == 1)
{
	motor[left_bttm] = 65;
	motor[left_mid] = 65;
	motor[left_top] = 65;
	motor[right_bttm] = 65;
	motor[right_mid] = 65;
	motor[right_top] = 65;
	 wait1Msec(100);
}
else
{
	motor[left_bttm] = 0;
	motor[left_mid] = 0;
	motor[left_top] = 0;
	motor[right_bttm] = 0;
	motor[right_mid] = 0;
	motor[right_top] = 0;
}

if(vexRT[Btn7U] == 1)
{
	motor[left_bttm] = 90;
	motor[left_mid] = 90;
	motor[left_top] = 90;
	motor[right_bttm] = 90;
	motor[right_mid] = 90;
	motor[right_top] = 90;
	wait1Msec(100);
}
else
{
	motor[left_bttm] = 0;
	motor[left_mid] = 0;
	motor[left_top] = 0;
	motor[right_bttm] = 0;
	motor[right_mid] = 0;
	motor[right_top] = 0;
}

Apparently, it works, but I for one am really confused as to how. Shouldn’t the motor values always reset to zero unless Btn7U is pushed, and if so, how do 7L and 7D work?

This is less a troubleshooting question and more of a “how did I get my programming knowledge wrong” question.

Thanks in advance for clearing up the confusion.

Strictly speaking the code should not work, however, it does.

The reason is that following the detection of a button push and setting the speed of the motors, the code makes a call to wait1Msec where the program is suspended for 1/10 second. When that delay has finished the code does send 0 to every motor due to the other two buttons not being pressed, however, this happens very quickly and (assuming this is all within a while loop) the code then returns to the if statement where the motors are again sent a speed value. The time where the motors have a valid control speed is much longer that the time where they have been sent 0. The motors will occasionally see that control value of 0 but it will not happen very often.

If I were to rewrite this code it would look as below. I use a function for sending values to the motors, the original has lots of duplicated statements which are a prime candidate to be simplified.

void
ControlMotors( int speed )
{
    motor[left_bttm]  = speed;
    motor[left_mid]   = speed;
    motor[left_top]   = speed;
    motor[right_bttm] = speed;
    motor[right_mid]  = speed;
    motor[right_top]  = speed;
}

task main()
{
    while(1)
    {    
        if(vexRT[Btn7D] == 1) {
            ControlMotors(45);
        }
        else
        if(vexRT[Btn7L] == 1) {
            ControlMotors(65);
        }
        else
        if(vexRT[Btn7U] == 1) {
            ControlMotors(90);
        }
        else {
            ControlMotors(0);
        }
    wait1Msec(25);
    }
}