<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Aug 10, 2015, at 4:45 AM, Rick Jansen <<a href="mailto:rick.jansen@xs4all.nl">rick.jansen@xs4all.nl</a>> wrote:</div>
  

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  
  <div bgcolor="#FFFFFF" text="#000000">
    <font face="Helvetica, Arial, sans-serif"><blockquote type="cite">Well, that was fun, or was
      it. Handling a rotary encoder with an Arduino can be done, but
      there's a lot to take care of! The signals from my (simple) €1,50
      encoder bounce all over the place. My current implementation uses
      one of the two interrupt pins of the Arduino, although a polling
      solution is possible as well. I'm not sure if it is because of the
      "quality" of this encoder, but at times there are nearly as many
      pulses clockwise as anti-clockwise..  I ended up counting both
      clockwise and anti-clockwise pulses, the greater of which
      determines actual direction. Even an "acceleration" is detected,
      if you turn the rotary fast the value will change more
      dramatically.<br>
      <br>
      It works quite well, but at the same time I wonder how other
      machines work, that track 8 or 16 rotary encoders.. An Arduino
      Mega has many more interrupt pins than the measly Arduino Uno, but
      still. Is there a clever trick to track many rotary encoders that
      I am missing?<br></blockquote><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">A few comments:</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">1) The biggest issue with rotary encoders is missing one or more states. There are only 4 states, and it's reliable to determine the direction if every state is handled, but missing just one state means that the direction is unknowable. In such a situation (missing a state), the decoder must wait until the next state occurs to discover the direction. If two states are missed then the decoder will incorrectly assume that the encoder was turned in the opposite direction.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">Judging by the sheer number of commercial products that will get confused and spin the data value in the opposite direction of the knob, my judgement is that it must be rather easy to miss a state. Your interrupt-based solution is possibly saving you from this particular failure mode.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">There are rotary encoders with 4 and 5 bits, making it (hopefully) impossible to become confused about the direction, even when missing 1 or 2 states. Unfortunately, these encoders cost way more than the 2-bit versions. I was unable to find any 3-bit encoders, which was surprising.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">2) Check out the schematic for the Oberheim Matrix-12, a famous synth with 6 rotary encoders and a hopelessly slow 8-bit processor. They use a D-type flop flop for each encoder, such that one line provides the clock, and the other line provides the data. In this circuit, the Q output of the flip flop is always '1' when turning one direction, or '0' when turning the opposite direction. The same two raw encoder bits are sent to a NOR gate and a second flip-flop that sets the output high when both lines are low. Presumably, this is the "start" of a click. The flip-flops can also be reset by the CPU. The whole thing is described in the Service Manual. You can either use discrete logic chips or a small PLA.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">3) The Freescale K6x family of ARM Cortex-M4 processors have FlexTimer units with external inputs that can be put into a rotary encoder mode. The K64 chip has four of these FTM units, but I'm not sure they all implement the rotary encoder option. Even if they did, that would only handle 4 encoders. I haven't seen this peripheral feature working yet, though, so I can't vouch for it.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">4) If you run out of interrupts, you can always combine them with external gates and feed fewer pins, or even just a single pin. When your processor responds to the interrupt, it can read several rotary encoder pins (preferably on a single port) to determine which one actually changed, and still be able to handle changes to multiple at once.</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">Brian Willoughby</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif">Sound Consulting</font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div><div bgcolor="#FFFFFF" text="#000000"><font face="Helvetica, Arial, sans-serif"><br></font></div></div></body></html>