mathertel / RotaryEncoder

RotaryEncoder Arduino Library
Other
342 stars 107 forks source link

Interrupt example not actually using interrupts #29

Closed gi1mic closed 3 years ago

gi1mic commented 3 years ago

The Interrupt example should be changed to use the following format in the setup section

     attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

i.e. attachInterrupt(digitalPinToInterrupt(PIN_IN1), checkPosition, CHANGE); attachInterrupt(digitalPinToInterrupt(PIN_IN2), checkPosition, CHANGE);

and then the line

     encoder.tick();

can be removed from the loop function.

As shipped the example does not use interrupts and simply polls encoder changes.

wotupfoo commented 3 years ago

In examples/InterruptRotator/InterruptRotator.ino the setup() calls attachInterrupt() and the checkPosition() interrupt routine is right above it in the code. So while calling .tick() method in loop() is pointless, it is interrupt driven by the checkPosition handler.

mathertel commented 3 years ago

First: The attachInterrupt was missing – now it is present now and should work. Update to version 1.5.2.

Just to verify your findings I did the following using an ESP8266 nodemcu board using the InterruptRotator.ino example:

Then I removed the modifications above and added the code around the tick() call in the loop() function:

This was generating much less HIGH/LOW changes but they occur. So why ? At least this means that the tick() call in the loop function has effect.

My guess: I haven’t analyzed all signals in parallel but as I have done some prior work I know that the signal on the input pins may change VERY fast as the contacts often do create some HIGH/LOW noise that is typically removed from input signals by debouncing algorithms. Here with the rotary library the “debouncing is built in” but when missing a signal because it is generated before the last one was handled will leave an input state not recognized. I expect that the tick in the loop() function will detect this even without interrupt.

My conclusion: as long as the additional tick() in the loop() function doesn’t hurt, leave it in.