Page 1 of 1

Reading rotary encoder (getting random values)

Posted: Sat Apr 16, 2016 9:48 am
by rebb
I'm trying to read rotary encoder with this simple code i found from Arduino forums:

Code: Select all

 
 n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
   } 
   encoder0PinALast = n;
Of course with similar xc code. But just getting random values when reading the pins. I also tried debouncing i'm using for buttons (just simple timer), but still cannot get steady reading. It is connected straight to 1-bit pins and middle pin to ground.

Encoder is Piher CI-11 (2 bit in quadrature code).

Does anyone have an example how to properly read the encoder or give any tips how to interface it properly to startKit and reading algorithm.

Re: Reading rotary encoder (getting random values)

Posted: Sat Apr 16, 2016 12:17 pm
by Ross
Can you post the rest of your code - the digitalRead() function for example.

Re: Reading rotary encoder (getting random values)

Posted: Wed Apr 20, 2016 4:36 am
by bearcat
In my use, a simple bit test didn't work. The switches are noisy. You need to use a state machine to detect valid transitions between the two channels (and reset on errors) to detect CW and CCW rotations. I did not need to de-bounce, but I did limit to 10mS reads. I don't have XC code for this routine, sorry (Microchip).