attachInterrupt() for encoder pulse counter not working

Can anybody tell me why my interrupt isn't working? I am very new to coding and am clearly missing something here because when running this code, the pins do not get interrupted but when testing the doEncoderA and doEncoderB, it counts the pulses. Any help would be greatly appreciated, thank you :slight_smile:

volatile long int PulseCount;
int encoderA=2;
int encoderB=3;

void setup()
{ pinMode(encoderA,INPUT_PULLUP);
pinMode(encoderB,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), doEncoderA, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), doEncoderB, CHANGE);
Serial.begin(9600);
}

void doEncoderA()
{

//if the value at channel A is not equal to the value at B
// that is a change in the number of ticks
if(digitalRead(encoderA)!= digitalRead(encoderB))
(
PulseCount++
);

else
(
PulseCount--
);

Serial.print("PulseCount: ");
Serial.println(PulseCount);

}
void doEncoderB()
{ //if the value at A does not equal the value at B
//Ticks will increase

if (digitalRead(encoderA)==digitalRead(encoderB))
(
PulseCount++
);
else
(
PulseCount--
);
//print current number of ticks in serial monitor
Serial.print("PulseCount: ");
Serial.println(PulseCount);

}

Read the first topics like "How to use this Forum", "How to attache code".

Don't use Serial.print inside the ISR.

I'm surprised that your code compiles since there is a semicolon in each interrupt handler that should cause a syntax error.

if(digitalRead(encoderA)!= digitalRead(encoderB))
(
   PulseCount++
 ); <--------- No semicolon allowed here
   
 else
 (
   PulseCount--
 );

<--------- No semicolon allowed here @skyvan: Why not?
Not only allowed, but essential, I'd say. Odd use of parentheses, though.

I'd be more worried about a lack of a loop function, and the presence of serial prints in interrupt context.

Please remember to use code tags when posting code.

I really don't know if the parentheses after the If/Else statements are legal, but it might be worth trying this format to see if it makes any difference:

if(digitalRead(encoderA)!= digitalRead(encoderB))  PulseCount++;
   
else PulseCount--;

Also, which Arduino are you using? And which encoder? If it's a mechanical encoder, you'll need to do something about switch bounce.

ShermanP:
I really don't know if the parentheses after the If/Else statements are legal,

They are.

I strongly recommend to use one of the encoder libraries instead, especially Paul Stoffregen's.

Which of the dozens of different encoders do you have?