Tech-One (Customer) asked a question.

How to use interrupts on the P1AM-100

I am having problems getting interrupts to work on the P1AM-100 The same code works fine on the ARDUINO UNO. I just change the pin assignments. I am using a rotary encoder switch and i can not get interrupt function to work.


  • FACTS_AdamC (AutomationDirect)

    Can you post a picture of your wiring as well?

  • FACTS_AdamC (AutomationDirect)

    I just re-read your code and I think your issue is actually the order of your GPIO setup statements. I believe you have to declare your pins as inputs before the attachinterrupt function. Try changing your code like below and let me know if that fixes it.

    1. // inputs;
    2. pinMode(pinA, INPUT_PULLUP); // Used by Encoder A DT:
    3. pinMode(pinB, INPUT_PULLUP); // Used by Encoder B CLK:
    4. pinMode(buttonPin, INPUT_PULLUP); // Push Button on encoder :
    5.  
    6. // Read the initial state of inputCL and
    7. // assign to pinAStateLast variable
    8. pinAStateLast = digitalRead(pinA);
    9.  
    10. // outputs;
    11. pinMode(LED_BUILTIN, OUTPUT); //Set LED (Pin 32) to be an output
    12. // Atach a CHANGE interrupt to PinB and exectute the update function when this change occurs.
    13. attachInterrupt(digitalPinToInterrupt(pinB), update, CHANGE);

     

    Expand Post
    • Tech-One (Customer)

      Hi AdamC,

      Yes that corrected my problem. Thank You, I appreciated you helping me.

      When I run the program the encoder count does not count every click and some times count in reverse. would you happen to know of a better way to decode an encoder ?

      Best Regards

      Tech-One

      • FACTS_AdamC (AutomationDirect)

        It's a little harder to diagnose that without it in front of me. The first thing I'd try is to debounce your signals(either in hardware or software, plenty of guides out there on both) to make sure any chatter on the switching isn't causing what you're seeing.

         

        Another option is to consider the P1-02HSC module. It's setup to capture quadrature signals like from a rotary encoder.

         

        Thanks

        Adam

        Expand Post
  • Tech-One (Customer)

    Please see attached drawing of the Rotary Encoder Switch Wiring.

    Thank You

    • GPIO wiring Model (1)
    • FACTS_AdamC (AutomationDirect)

      Thanks for the diagram! I'd try the code change from my other comment. If that doesn't work, could you share the actual part number for the encoder?