[TriEmbed] C Code Question Ralated to Rotary Encoder
Dwight Morgan
dwight.w.morgan at gmail.com
Mon Feb 22 21:10:34 CST 2016
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.
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.
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.
The pointer works fine and prints out either a 1 or -1 on the serial
monitor.
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?
int bump[] = {0,0,-1,1};
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.
Thanks to C code by Budd Churchward on YouTube.
Input appreciated. Thanks!
Dwight
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20160222/c0ba0ce8/attachment.htm>
More information about the TriEmbed
mailing list