<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.hoenzb
        {mso-style-name:hoenzb;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Thanks Rodney and Mike for the responses. In Mr. Churchward’s code he declares the byte variable “state” and uses the function “knobTurned()” as follows – code snippets: <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>byte state = 0;           // will store </span><b><u><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:red'>two bits</span></u></b><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:red'> </span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>for pins A & B on the encoder which we will get from the pins above<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>void knobTurned(){<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  /* AH HA! the knob was turned */<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  state = 0;    // reset this value each time<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  state = state + digitalRead(pinA);   // add the state of Pin A<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  state <<= 1;  // shift the bit over one spot<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  state = state + digitalRead(pinB);   // add the state of Pin B<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  /* now we have a two bit binary number that holds the state of both pins<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     00 - something is wrong we must have got here with a key bounce<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     01 - sames as above - first bit should never be 0<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     10 - knob was turned backwards<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     11 - knob was turned forwards<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     */<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>     <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  /* We can pull a value out of our truth table and add it to the current level */<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  level = level + bump[state];<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  /* Let's see what happened */<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  Serial.print(bits[state] + "    ");  // show us the two bits<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  Serial.print(bump[state],DEC);       // show us the direction of the turn<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  Serial.print("    ");<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>  Serial.println(level);               // show us the new value<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I’ve never used bytes much in code and never really  thought about it much and I always saw calls to specific array elements with a decimal value in the square brackets like this: X = myarray[2]; for instance to get the 3rd element value. <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>It looks like when a byte is used as the index the value is a binary number – using ones and zeros – and it can simply because it is a byte!<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>If you look at the knobTurned() function it is building a two place binary index stored in the byte variable named “state”. When the code sees the binary index as a byte it knows that 10 means read the third element value and when it sees 11 it knows to read the fourth element value. <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>This seems like a very handy thing to know for new learners like me with Arduino and RPi. <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>That’s my now understanding of it. Please shim up any parts as needed!<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Thanks again!<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Dwight<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Rodney Radford [mailto:ncgadgetry@gmail.com] <br><b>Sent:</b> Monday, February 22, 2016 11:22 PM<br><b>To:</b> Dwight Morgan<br><b>Cc:</b> Triangle Embedded Computing Discussion<br><b>Subject:</b> Re: [TriEmbed] C Code Question Ralated to Rotary Encoder<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal>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">https://www.youtube.com/watch?v=FGxLXqzPe3Q</a><o:p></o:p></p></div><p class=MsoNormal>     <o:p></o:p></p></div><p class=MsoNormal>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<o:p></o:p></p><div><div><div><div><p class=MsoNormal style='margin-bottom:12.0pt'><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.<o:p></o:p></p></div><div><p class=MsoNormal>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.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-bottom:12.0pt'><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<o:p></o:p></p></div><div><p class=MsoNormal style='margin-bottom:12.0pt'>Here we just print out the integer level variable that has already been incremented or decremented.<br><br><o:p></o:p></p></div></div></div></div></div><div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>On Mon, Feb 22, 2016 at 10:10 PM, Dwight Morgan via TriEmbed <<a href="mailto:triembed@triembed.org" target="_blank">triembed@triembed.org</a>> wrote:<o:p></o:p></p><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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. <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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. <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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. <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>The pointer works fine and prints out either a 1 or -1 on the serial monitor. <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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? <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>int bump[] = {0,0,-1,1};  <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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. <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Thanks to C code by Budd Churchward on YouTube.<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Input appreciated. Thanks!<o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:#888888'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:#888888'>Dwight<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:#888888'> <o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:#888888'> <o:p></o:p></span></p></div></div><p class=MsoNormal style='margin-bottom:12.0pt'><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" target="_blank">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>TriEmbed web site: <a href="http://TriEmbed.org" target="_blank">http://TriEmbed.org</a><o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>