Define SIMULATION_WAITMS_VALUE = 1 '<<<<<<<<<<<<<<<<<<<<<<<<<<< Define SINGLE_DECIMAL_PLACES = 2 Define CLOCK_FREQUENCY = 8 Define CONFIG1L = 0x00 Define CONFIG1H = 0x08 Define CONFIG2L = 0x0c Define CONFIG2H = 0x20 Define CONFIG3L = 0x04 Define CONFIG3H = 0x80 Define CONFIG4L = 0x80 Define CONFIG4H = 0x00 Define CONFIG5L = 0x0f Define CONFIG5H = 0xc0 Define CONFIG6L = 0x0f Define CONFIG6H = 0xe0 Define CONFIG7L = 0x0f Define CONFIG7H = 0x40 Dim position As Word 'position var Word Dim target As Word 'target var Word Dim distance As Word 'distance var Word Dim decposition As Word Dim absdistance As Word Dim flag As Bit WaitUs 100 Hseropen 9600 WaitMs 100 position = 0 '1234 main: Hserout 13, "You're at position: ", decposition, 13, 10 '13=carriage return 10=line feed Hserout "Enter new target: ", 13, 10 '13=carriage return 10=line feed Hserin target, Hserout "target ", target, 13 '13=carriage return 10=line feed 'Actual calculation starts here. 'When done the variable Distance contains the 'number of "ticks" to target in two's complement format. distance = target - position If distance.15 = 1 Then 'Distance is negative flag = 1 distance.15 = 0 'you could try, as I am not sure what the limits of the distance value distance = 65535 - distance If distance > 1791 Then distance = distance + 3582 Endif Endif If distance.15 = 0 Then 'Distance is positive If distance > 1791 Then distance = distance - 3582 Endif Endif If distance.15 = 1 Then 'Distance is negative If absdistance > 1791 Then distance = distance + 3582 Endif Endif 'End of calculation, result now in Distance. done1: 'was 'done' If flag = 1 Then 'Distance is negative Hserout "ccw ", 13 Else Hserout "cw ", 13 Endif 'Hserout[dec abs distance, " ticks.", 13] position = target Goto main