Read encoder with interrupt and output max7219 drive

Hi. I configured an arduino nano to read an encoder in pin 2 and 3 with interrupts and show the value on a 4 digit display using TM1637 library and it worked fine.
But today I tried to change the display to a MAX7219 (LedControl lib) and now it loses counts and I cannot find the source of the problem.
Below the code.
pin 11 is connected to the CLK(13)
pin 10 is connected to LOAD(12)
*/
LedControl lc=LedControl(12,11,10,1);

const byte encoderPinA = 2;//outputA digital pin2
const byte encoderPinB = 3;//outoutB digital pin3
volatile int count = 0;
int protectedCount = 0;
int previousCount = 0;
int showit = 0;

#define readA bitRead(PIND,2)//faster than digitalRead()
#define readB bitRead(PIND,3)//faster than digitalRead()

void setup() {

lc.shutdown(0,false);
/* Set the brightness to a medium values /
lc.setIntensity(0,5);
/
and clear the display */
lc.clearDisplay(0);

pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);

attachInterrupt(digitalPinToInterrupt(encoderPinA), isrA, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), isrB, CHANGE);

}

void loop() {

noInterrupts();

protectedCount = count;

lc.setDigit(0,3,(showit / 1000),false);
lc.setDigit(0,2,((swowit / 100) % 10),false);
lc.setDigit(0,1,((showit / 10) % 10),false);
lc.setDigit(0,0,(showit % 10),false);
delay(100);

interrupts();

if(protectedCount != previousCount) {
showit = protectedCount;
}
previousCount = protectedCount;
}

void isrA() {
if(readB != readA) {
count ++;
} else {
count --;
}
}
void isrB() {
if (readA == readB) {
count ++;
} else {
count --;
}
}

There is no need to have your interrupts off while setting the digits of the display or doing a delay(100)

1 Like

You only need the interrupts off for this one line so you should not keep them disabled for other stuff.

1 Like

yes ! thank you very much.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.