PLTW Elevator: Encoder Issue

We are currently working on our PLTW Elevator code and use encoders to track our progress. When we press level two, the motor will run until the level is reached. Once we hit another button, the encoder will restart the count. What could be cause the encoder to reset.

#pragma config(Sensor, dgtl1, Bump1, sensorTouch)
#pragma config(Sensor, dgtl2, Bump2, sensorTouch)
#pragma config(Sensor, dgtl3, Bump3, sensorTouch)
#pragma config(Sensor, dgtl10, encoder, sensorQuadEncoder)
#pragma config(Motor, port2, motor, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
while(true)
{
if(SensorValue(Bump1) ==1)
{
startMotor(port2, -127);
untilEncoderCounts(-8,dgtl10);
stopMotor(port2);
}
if(SensorValue(Bump2)==1)
{
if(SensorValue(encoder)<445)
{
startMotor(port2, 127);
untilEncoderCounts(450,encoder);
stopMotor(port2);
}
else //if(SensorValue(encoder)>445)
{
startMotor(port2, -127);
untilEncoderCounts(440,encoder);
stopMotor(port2);
}
}
if(SensorValue(Bump3)==1)
{
if(SensorValue(encoder)<=850)
{
startMotor(port2, 127);
untilEncoderCounts(850,encoder);
stopMotor(port2);
}
}
}
}

Because every time you use untilEncoderCounts(n,encoder) all it is doing is counting the from its current position. It is not resetting per se, it is merely counting from where it is. So on the second push of the button it will travel up 450 counts, not to a position of 450 relative to the “floor”. You need to define the counts as relative to the current position of the robot, not relative to the “floor” = 0.
I hope this makes sense.

The untilEncoderCounts() function resets the encoder each time you use it. If you right click on the function and click “go to definition” it shows what the function does. It also uses the absolute value of the count, so it doesn’t differentiate between up and down in your program.