Incremental rotary encoder with 128 PPR? For MIDI CC

If you are using interrupts to service an encoder, I've worked out a way to virtually eliminate interrupts generated by bouncing. The result is that for a typical encoder you have just four interrupts per detent - one for each real transition of a switch.

The idea is to enable the interrupt on only one pin at a time. When that interrupt occurs, in the ISR you disable that interrupt and enable the interrupt on the other pin. So any bouncing that occurs on the pin that just generated the interrupt won't generate any more interrupts because interrupts on that pin have been disabled. The other pin should have changed state some time ago, and should be stable.

There's a complication when you change directions because the interrupt is enabled on the wrong pin. But this is solved by increasing the lookup table to 32 bytes, and including the indentity of the interrupting pin as the fifth bit of the index value.

Another nice feature of this method is that you can eliminate false readings caused by bouncing which takes place between the time the interrupt is triggered and the time the port is read. Since you know which pin generated the interrupt, and can keep track of which change it had to make to do that, you know what value it must have had to generate the interrupt, and you can use that value instead of the value read from the port, which may be wrong.

I've written demonstration sketches using both pin change interrrupts and the external interrupts on D2 and D3 (ATmega328P). At each detent they print to the serial monitor the actual number of interrupts which have been serviced since the last detent. For comparison, I wrote the same feature into the standard lookup table method, which leaves interrupts enabled on both pins all the time, and which therefore has to service all the interrupts generated by bounces. So you can compare the results you get for a particular encoder. These sketches and a PDF explaining everything can be found in the Arduino folder (ignore everything else) of my Github on this subject:

https://github.com/gbhug5a/Rotary-Encoder-Servicing-Routines

I seriously doubt I actually invented this method, but after looking a good bit couldn't find it anywhere, so I took a shot at it myself. Anyway, I think this method would work well with the variable speed idea. Also, I'd like to get some feedback on how well it works for others. I get essentially perfect results, with four interrupts per detent, on the two cheap standard encoder modules I got from Banggood, but would be interested in knowing how well it works with other encoders.