How fast are you turning the encoder? With slow speeds this code can work. But if you are turning the encoder very fast at all then it is often necessary to use interrupts to catch all the transitions. Here is some example code that handles encoders from an interrupt using direct port reads to be sure to catch everything:
The shortest negative pulse is 12.45ms at this speed, it missed 4 out of ten steps. I would not say this is fast.
I count only one transition not the complete movement because i need one step per click. In case it wasn't clear i want to use it to change radio frequency or the altutide setting for the autopilot etc.
The reason i wanted to write my own code is i need at least 6 possibly 8 encoders and interupt pins are scarce. I had success with a found example with interrupts.
My two questions: can you explain to me WHY my code is too slow (even when there is nothing else in the loop)
If i need that many encoders what design options i have? I have a few ideas but so far lucked out and would be nice to hear from someone more experienced.