Appropriate Method of Counting counts from a Quadrature Encoder
It's best if you have quadrature encoder hardware to handle the encoder pulses, unless the speed is very low. Many microcontrollers have at least one such peripheral on the chip.