I'm clear once we test to see if a change has occurred [if (newA != oldA || newB != oldB)], but it seems to me that we then only calculate a result for the case when we are transitioning from Phase 2--->Phase 3 or Phase 1--->Phase4 [if (oldA == LOW && newA == HIGH)]. This ignores valid transitions when aPin goes from HIGH to LOW and transitions where aPin stays the same and bPin goes HIGH to LOW or LOW to HIGH, all of which are valid state changes that in this code will result in result being 0, incorrectly signalling no change in the rotary encoder at all.
I think this code drops 75% of the state transition cases, but it's possible I've lost the plot here!
Can anyone shed some light? Many thanks,
The code will work reliably if the direction never changes, only counting 50% of the transitions. However when it reverses it miscounts by
1. If it reverses several times with only a little movement it will repeatedly miscount.
The flaw with this approach is that if you only look at transitions where A changes, you must take account of both 0->1 and 1->0 transitions
Correct code would be something like
if (newA != oldA)
result = newA ^ newB ? -1 : 1 ;
else if (newB != oldB)
result = newA ^ newB ? 1 : -1 ;