Multiples Incremental Rotary Encoders

I wrote this and I can connect one encoder with success, but the same code cant execute in deferente port (otherwise 2,3), how can I connect 3 encoders? diagram and code fix

byte A = 2;
byte B = 3;
volatile int counter = 0;

void setup() {

    // set DIO pins
    pinMode(A, INPUT);
    pinMode(B, INPUT);

    // Turn on pullup resistors
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);

    // Attach interrupt to pin A
    attachInterrupt(digitalPinToInterrupt(A), isr, CHANGE);

    // Use serial port to keep user informed of rotation
    Serial.begin(9600);
}

void loop() {
}

void isr() {
    if (digitalRead(A) == digitalRead(B)) {
        counter++;
    } else {
        counter--;
    }
    Serial.println(counter, DEC);
}

I assume you are using an Uno or Mega which uses the AVR 328 series CPU. That chip only has external interrupts on pin 2,3. That is why it won't work on other pins.

Thanks for reply aarg , the board is a leonard...

If I connect my encoder 1 in A=0 and B=8 and my encoder 2 in A=1 and B=9 I will reach my goal?

I read, some minutes ago that leonard uses 0,1,2,3,7 with interrupts it is right?

Why the split, one interrupt and one not? I'm checking the Leo now... I guess that board would support two encoders, example 0,1 and 2,3.
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

Edit: I'm not saying maybe more aren't possible, it's obvious that interrupts would help with a device like that. There have been fairly recent discussions on this forum recently about using other pins, e.g. pairs of one interrupt and one non-interrupt pin on each encoder. Maybe that's what you referred to earlier.

I guess you could say, a non-interrupt or hybrid single external interrupt driven system, would have a much higher software overhead than the code you've posted. There are a lot of processors now that have far more external interrupts available, too.

You could probably find some library that has the capabilities, you can test them all.

Yeah... Using only one interrupt port for each encoder, with mode setted as CHANGE, we can check the par os pins and decide to increment and decrement chechking diferenc between both.... I made some tests now and I read 2 encoders. When I wrote this post I was in doubt about interrups, I guess now I understand it. Thanks you for envolving with my issue.

This is a very clean code to read encoders, can help others... with Leonardo we can read 5 encoders. 0,1,2,3,7, each one as interrupt pins at channel A and with another digital pin as channel B to make a caple.

we can set variables as arrays and make a main isr with array index and a method to call isr with indes to be signed in attachInterrupt() method

Now I can put this code inside a community library to build a flight simulator joystick with force feddback... going to motors hardware and code now.

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