One idea: are you using volatile variables for the flag and position and having critical section in the loop when you reset the flag or update position (given Your handler is called from an interrupt) ?
One idea: are you using volatile variables for the flag and position and having critical section in the loop when you reset the flag or update position (given Your handler is called from an interrupt) ?