<div dir="ltr">The best explanation is visual. Quadrature is two square waves 90 deg out of phase. This is done by two physical optical slits on the rotary encoder with a through beam optical coupler. Here's the image that helps understand up-down counting<div><br></div><div><img src="cid:ii_1530e29d4acff1ad" alt="Inline image 1" width="475" height="267"><br></div><div>you can see 2 lows 00, high-low 10, high-high 11, and low-high 01 as an ever repeating pattern as the rotary encode moves in one direction or the other. A glitch-free encode is necessary to count up or down on a state transition as the input is too fast for almost any general purpose cpu (that's not dedicated to counting). </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 22, 2016 at 11:21 PM, Rodney Radford via TriEmbed <span dir="ltr"><<a href="mailto:triembed@triembed.org" target="_blank">triembed@triembed.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>I had to search for the video you watched to see what was being done and then find the code.  For those following along, the video is part of a 3 part video of using a quadrature rotary encoder as an input for a morse code sending device.  The one necessary for this discussion is the part 2 which can be found at <a href="https://www.youtube.com/watch?v=FGxLXqzPe3Q" target="_blank">https://www.youtube.com/watch?v=FGxLXqzPe3Q</a><br></div>     <br></div>In the video, as the knob is turned to the left or right, a line is printed with three values on it, like this:<br><br>11      1      70<br>11      1      71<br>10      -1     70<br>10      -1     69<br><div><div><div><div><br>The first column represents the two bits of the quadrature, the second column indicates whether we are counting up (+1) or down (-1) and the third column represents the current value.<br><br>The question is how can the compiler know that the 11 represents a binary number whose value is 3, and how does it know that 10 represents a binary number whose value is 2.<br><br></div><div>The answer is that it doesn't have to... the internal variable is called state and it either has a 0, 1, 2, or 3.<br></div><div><br>When printing, the author decided to show the value of the number in binary, instead of decimal, so he printed the first value like this:<br><br>   /* For demo purposes we will create an array of these binary digits */<br>   String bits[] = {"00","01","10","11"};<br><br>   /* Let's see what happened */<br>   Serial.print(bits[state] + "    ");  // show us the two bits<br><br>So as you see, the number is the index into a character string array and it prints out either the string "00", "01", "10", or "11".  This is just to make it easier for you to visualize that the number is really a two bit number representing the two outputs of the quadrature knob.<br><br>The second value in the line is printed with this line (this is the one you showed).  This prints out only the <br><br>   int bump[] = {0,0,-1,1};<br>   Serial.print(bump[state],DEC);       // show us the direction of the turn<br><br>Here we see the same 0..3 state variable indexing into a numerical array and printing out either 0, 0, -1 or +1 as a DECimal number.<br><br>And then the third value is printed with these lines:<br><br>    Serial.print("    ");<br>    Serial.println(level);               // show us the new value<br><br></div><div>Here we just print out the integer level variable that has already been incremented or decremented.<br><br><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Mon, Feb 22, 2016 at 10:10 PM, Dwight Morgan via TriEmbed <span dir="ltr"><<a href="mailto:triembed@triembed.org" target="_blank">triembed@triembed.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div link="blue" vlink="purple" lang="EN-US"><div><p class="MsoNormal">I’m trying to understand and use a rotary encoder on an Arduino project and I have a simple demo circuit working with the encoder and output to the serial monitor. I see how the code is working but I can’t find documentation that confirms what I’m seeing. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The C code builds a two digit binary number that is used as a pointer to values in an array that is used to output either a 1 or -1 to indicate if the encoder is rotating clockwise or counterclockwise – all well and fine. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The variable used as a pointer is a type byte initially set to zero (byte state = 0) with a function called by an interrupt to build the two digit pointer. I looked it up and a byte can either hold 8 bits or a decimal value from 0 to 255. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The pointer works fine and prints out either a 1 or -1 on the serial monitor. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">My question is, how does the code know that a binary 10 is not the number ten as the pointer or binary 11 is not the number eleven as the pointer instead of knowing it is the number 2 or 3 to be used as the pointer to pick out values in the following array? <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">int bump[] = {0,0,-1,1};  <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The output is like this: Serial.print(bump[state]),DEC);  //state being the built pointer of either binary 10 or 11 for a value of either decimal 2 or 3, respectively. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Thanks to C code by Budd Churchward on YouTube.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Input appreciated. Thanks!<span><font color="#888888"><u></u><u></u></font></span></p><span><font color="#888888"><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Dwight<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p></font></span></div></div><br></div></div>_______________________________________________<br>
Triangle, NC Embedded Computing mailing list<br>
<a href="mailto:TriEmbed@triembed.org" target="_blank">TriEmbed@triembed.org</a><br>
<a href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org" rel="noreferrer" target="_blank">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
TriEmbed web site: <a href="http://TriEmbed.org" rel="noreferrer" target="_blank">http://TriEmbed.org</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Triangle, NC Embedded Computing mailing list<br>
<a href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a><br>
<a href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org" rel="noreferrer" target="_blank">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
TriEmbed web site: <a href="http://TriEmbed.org" rel="noreferrer" target="_blank">http://TriEmbed.org</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Best regards,  Mike</div>
</div>