Encoder Library w/ ENCODER_DO_NOT_USE_INTERRUPTS to 2x or 1x count?

Dave X

Well-known member
I own a 1000 Line-Per-Revolution/ 1000 Pulse Per Revolution / 4000 A/B edges Per Revolution optical encoder that looks like it tops out around (100Kint/sec)/(4000int/rev)*60sec/min = 1500RPM if I hook to interrupt-capable pins on a 16MHz Uno or Teensy 2.0. I'd like to downgrade the resolution at high speeds without switching pins.

In the code for the encoder library, there's a #define for ENCODER_DO_NOT_USE_INTERRUPTS that inhibits use of interrupts and falls back to polling with encoder.read()

Could one use it to degrade the encoder library's 4-count-per quadrature cycle to a 1 count/single interrupt per quadrature cycle?


... now that I ask, 2x seems very doable with something like this modification of https://github.com/PaulStoffregen/Encoder/blob/master/examples/NoInterrupts/NoInterrupts.pde to call encoder.read() based on an interrupt:


Code:
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

long newPos;

void myRead(){  
  newPos = myEnc.read();
}

void setup() {
  Serial.begin(9600);
  Serial.println("Basic NoInterrupts + 1x Reading Test:");
  attachInterrupt(digitalPinToInterrupt(5), myRead, CHANGE);
  myRead();
}

long position  = -999;

void loop() {
  //long newPos = myEnc.read();
  if (newPos != position) {
    position = newPos;
    Serial.println(position);
  }
  // With any substantial delay added, Encoder can only track
  // very slow motion.  You may uncomment this line to see
  // how badly a delay affects your encoder.
  //delay(50);
}

1x reading by triggering on RISING doesn't look like it wouldn't work, since the library assumes no motion if the bit pattern is the same point in the cycle at each read().

But it looks like you can double the top encoder spindle speed through trading off resolution for processing time by either selecting a non-interrupt pin for one quadrature channel, or by disabling interrupts within the library and rolling your own interrupt calling read().
 
Back
Top