Use modulo arithmetic. // Pseudo-code // error error = (setPoint + 512 - currPos) % 1024 - 512; This code shifts the reading from {0 - 1023} to {512 - 1535}, subtracts the previous reading and gets the modulus. The answer will be in the range 0 - 1023 but will have a positive offset of 512 which is subtracted out. >Suppose my current position is 10 and setpoint is 1023. The error is 1023 - 10 = 1013 ... error = (setPoint + 512 - currPos) % 1024 - 512; // Calculation error = (1023 + 512 - 10) % 1024 - 512 = 1525 % 1024 - 512 = 501 - 512 = -11