Reading rotary encoder (getting random values)

All technical discussions and projects around startKIT
Post Reply
rebb
Member
Posts: 9
Joined: Thu Jun 25, 2015 12:14 pm

Reading rotary encoder (getting random values)

Post 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.


User avatar
Ross
XCore Expert
Posts: 962
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

Can you post the rest of your code - the digitalRead() function for example.
bearcat
Respected Member
Posts: 283
Joined: Fri Mar 19, 2010 4:49 am

Post 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).
Post Reply