Elevator program using count and modulus

Students created a machine that lifts a weight. It starts when button is pushed, stops when reaching upper limit, reverses direction on next button push, stops when reaches lower limit switch. This is a continious loop. Problem we are having is direction does not always change. Can you please help me help my students?

task main()
{
int countValue=3;

while(1)
{
if(SensorValue[button]==1)////////////////////////////PUSH THE BUTTON IT RUNS ALL MOTORS POSITIVE DIRECTION
{

	startMotor(rightMotor,127);
	startMotor(leftMotor,127);
	startMotor(midMotor,127);

	countValue=countValue+1;///////////////////////////////ADDS 1 TO THE COUNT


}
else if (countValue %2==0)
{
	startMotor(rightMotor,-127);
	startMotor(leftMotor,-127);
	startMotor(midMotor,-127);

countValue=countValue+1;

}
else if(SensorValue[upper]==1 ||
SensorValue[lower]==1)
{
stopMotor(leftMotor);
stopMotor(rightMotor);
stopMotor(midMotor);
}

}

}