Hi Tim,
Looks like you figured out the ATmega pin numbers and corresponding hex codes properly. The code you updated inside the Interrupt Service Routines (ISR) also looks OK. What is missing in your code is “attaching” the ISRs to the right* interrupts so the right* one fires when each of the encoder pins change. (* right here means “appropriate”, not the opposite of left…)
For your left encoder, you are now using external interrupts INT0 and INT1 so you cannot use ISR(PCINT0_vect)
which fires when enabled pin change interupts (PCINT0-7) change state. Instead, you need to create a new ISR function:
static void leftISR() {
// Code for left encoder interrupt handling here
}
and attach it to the pins corresponding to INT0 and INT1 in the constructor:
attachInterrupt(digitalPinToInterrupt(LEFT_A), leftISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(LEFT_B), leftISR, CHANGE);
You can also get rid of the two following lines in the constructor as the lines above already take care of it.
PCMSK0 |= (1 << INT1);
PCMSK0 |= (1 << INT0);
For your right encoder, it is a bit more complicated since you are using both an external interrupt (INT6) and a pin change interrupt (PCINT4). For this reason, you will need two ISRs to handle the different types of interrupts. Both will use the same code inside the ISR body.
To handle changes on INT6 (right pin A), you can keep the rightISR()
function as-is. In the constructor, you can remove the following line as you will handle the changes on your right pin B differentlly:
attachInterrupt(digitalPinToInterrupt(RIGHT_B), rightISR, CHANGE);
To handle changes on PCINT4, you will need to use the ISR defined by ISR(PCINT0_vect)
which was previously used to handle pin changes for the left encoder. Simply replace the code in this function by the same code that is in rightISR()
.
Finally, in the constructor you need to enable PCINT4 by setting the corresponding bit to 1 in the PCMSK0 register:
PCMSK0 |= (1 << PCINT4);
Hopefully that should get it working. I couldn’t try it out as I would have to re-wire my robot. If you still run into issues, let me know and I will try to help!
Looking forward to see the results of your work! Make sure to post in the “Share you project” section of the forum when you have it working.