Encoder icon indicating copy to clipboard operation
Encoder copied to clipboard

seems like you've overoptimized update() function

Open celesteking opened this issue 6 years ago • 0 comments

Hi. In no-interrupts case, when you invoke .read() inside the ISR of pin change interrupt, something bad happens. ISR is only fired once and then, no new calls happen. Funny thing is that adding some other function right below the read() call will make it work normally. I guess it's related to the ASM code in update().

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>

Encoder myEnc(A2, A3);

void setup() {
  Serial.begin(115200);
  while (!Serial); Serial.println("Started.");
  PCMSK1 |= bit(PCINT10) | bit(PCINT11) | bit(PCINT12);
  PCIFR  |= bit (PCIF1);   // clear any outstanding interrupts
  PCICR |= bit(PCIE1);
}

volatile int pos  = 0;
int oldpos = 0;

void loop() {
  if (oldpos != pos) {
    oldpos = pos;
    Serial.println(pos);
  }
  delay(100);
}

// PCINT10, PCINT11
ISR(PCINT1_vect) {
  pos = myEnc.read();
  //  adding digitalRead(A0)
  // or even Serial.println(pos) updates the var correctly
}

celesteking avatar Aug 07 '19 15:08 celesteking