@marcus barnet: I think I misuderstood your initial post - you are not measuring the motor
position with the encoder at all (which is 99% of the cases using an encoder).
Your code is rather spaghettified - it needs to be split up into small, well-named functions, then
you (and others) can actually read it and understand it.
Very good idea to use a stepper library here, and a variable to hold the current desired position.
I'd recommend AccelStepper and then you can call moveTo() every time the encoder value changes,
very simple.
@Mydakota: post all the details of your encoder and motor please...