Problem with Handling 7 Encoders with interrupts on Arduino Every

Hey Together,

I started a Project where I need to read in 7 Digital Encoders, each with 2 Channels (Differential).
I found the Arduino Nano Every and was quite thrilled that all Digital Pins are capable of Interrupts. [attachInterrupt() - Arduino-Referenz]

I built up a the prototype using LM393n Comparators for the differential Signal and Pull-Up's to Vcc to get "0V" or roundabout "5V" from each channel(Arduino input threshold supposedly is 3.5V so we should be golden on this front). I tested every Channel with my picoscope and it seems to work like a charm. Basic sketch of each channel in appendix

From a Sofware point of view this principle works also well on the native "Digital-Only Pins" (D2-D12). Here I get my interrupts while moving my encoders, and the system is quite when not.
It does not work on "Analog and Digital Pins" (D14-D17). Here interrupt is constantly triggered. Even when I pull the Pin down to GND. If I disable the Pin 19 the same problem occurs on pin 20. I think I simply don't grasp how to use the Pins properly. But I also can't find a good fix.
I tried to use the "MegaCoreX-Lib" and use those Analog inputs as comparators themselves. But no luck there. It just trows out more issues. (Also it's quite unpretty to look at codewise)

I Put my code last coz It's probably just caused by my wrong understanding of the pins. I put a snippit here. Code is the same for all Pins.

pinMode(19, INPUT);
attachInterrupt(digitalPinToInterrupt(19), interruptFunction, Change);

//Other Stuff

void interruptFunction(){
     if(Enc_X_Ch_A_isLeading){
         counterEncX++;
     }
     else{
         counterEncX--;
     }
}

so I hope this Post contains anything thats needed. I am happy to provide more Info if needed.

thanks for your time and help!

cheers!

Kevin

2021-03-03 09_36_54-Da Vinci_electric - PDF-XChange Editor.png

@kevin_1991

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Digital encoders have two outputs that are not differential but 90º out of phase, so the outputs perform a grey code count as the shaft is turned -- 00 01 11 10 00 ... in one direction and 00 10 11 01 00 in the other. So you don't want to use a comparator but bring both both outputs to digital pins. Then you can have each one cause an interrupt on change. In fact they can both share a single interrupt service routine since only one changes value at a time:

volatile int position;
void interruptFunction(void) {
  // phase A or phase B has changed, but we don't know which!
  static bool lastPhaseA, lastPhaseB;
  bool a = digitalRead(phaseAPin);
  bool b = digitalRead(phaseBPin);
  if (lastPhaseA != a) {
    position += (a == b) ? -1 : 1;
    lastPhaseA = a;
  }
  if (lastPhaseB != b) {
    position += (a == b) ? 1 : -1;
    lastPhaseB = b;
  }
}

Isn’t Change supposed to be CHANGE? It’s an enum and C/C++ is case sensitive

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