Is it possible to use interrupts with a rotary encoder where both CLK and Data pins use only one hardware interrupt pin?
Reason being that ATmega328 has only two interrupt pins and one is already in use in my project.
You can select one pin for the clock signal, with interrupt.
Or you use PCINT with one or all pins.
Try it and tell us about.
I think so, if you use an 'XOR-gate' to trigger the interrupt (set for 'CHANGE' and then read the 2 pins individually to determine the direction. What is the other interrupt for ?
probably the easiest solution.
As @DrDiettrich mentioned PCINT may be the easiest approach.
This source may be helpful
https://www.electrosoftcloud.com/en/pcint-interrupts-on-arduino/
[Edit]:
There seems to be a typo in one of the examples on the linked page:
else if (digitalRead(5) != pin4) {
Serial.print("Pin5 has changed state and its new state is: ");
Serial.println(!pin5);
pin5 = !pin5;
}
}
should read
else if (digitalRead(5) != pin5) {
Serial.print("Pin5 has changed state and its new state is: ");
Serial.println(!pin5);
pin5 = !pin5;
}
}
Unfortunately that website times out when I tried to reply directly ...
The other hardware interrupt is used for keypad stroke detection.
Thank you everyone, I will study the PCINT route.
no, atmega328 has interrupts on each digital pin
Each digital pin can cause an interrupt, but a PCINT handler is invoked for a port, not for a specific pin.
agreed, but the handler can be used with any pin, right?
Yes but the handler will need to figure out which pin changed state and caused it to be triggered so it can take appropriate action.
Do you need interrupts for that.
Is a super-human pressing the keys, or is the loop time slow.
Leo..
Good question, but I am not sure I understand:
I thought that if no interrupt is used for keystroke detection that the keypad needs to be polled continuously thereby occupying the loop..??
The loop time is not slow at all. The purpose of the program is to drive a stepper motor depending on one single keypad stroke: about 9 keys for each stepper motor position.
One single stroke every few minutes at the very most.
So I should get rid of the interrupt for that?
I don't know if that's a problem. Depends on the rest of the code, and the step frequency.
When I wrote code for 48 steppers on a single classic Nano I had about a millisecond spare between a single step of the 48 motors. You can poll a lot of buttons in 1ms.
Leo..
All that the program does is wait for one single keystroke which lasts at least 50 to 100ms and then steer a stepper (with the accelstepper.h library).
I will abandon the interrupt usage for that. I would publish my code here but it is such a mess at the moment.
Thanks for that enlightening idea Leo!
Erik
Action could be taken on the first button push detection, and then ignored for some time.
See the StateChange example that comes with the IDE.
Leo..
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.