dimircea / REncoder

Rotary Encoder with and without builtin Switch implementation for Arduino UNO/MEGA/DUE and many others, inclusive Seeeduino XIAO.
Other
3 stars 1 forks source link

Update internal encoderPosition value #2

Open adynis opened 3 years ago

adynis commented 3 years ago

Question: Is it easily and officially possible to change encoderPosition (stored by the rEncoder object) value in IRQ mode?

Background: I use the rotary encoder to change the duty cycle (PWM) of some LEDs. But I do this also via the web interface. I was thinking on which variant it's better to sync hardware button value with web-based value: to have another global variable for the rotary in order to detect if I should increase / decrease the duty cycle? or to update the internal encoderPosition based on the web input ...

dimircea commented 3 years ago

Hi,

Yes, you can programmatically enforce the value of the encoder position. Please have a look at the REncoderWithoutSwitch.h and you can see the void setPosition(RENCODER_ROTATION_VALUE_TYPE encoderPosition); method. However, this requires to not have the IRQs enabled for it to work. Disabling the IRQs is not necessarily a bad thing if your board is capable enough to process all the code that you need to run, including the REncoder code of course. Practically, IRQs are recommended but not a strict requirement if your board is capable enough. An ATmega 328p (e.g., Arduino UNO or alike) is somewhere at the limit if you have some serious code running there, but an ESP8266/32 and or an Arduino MEGA will normally have no issue with that.

However, in general I will advice to use this feature ONLY if you have no other better option, since this interferes with the logic of encapsulation of the encoder. The encoder (with or without a push switch) is a mechanical device with a digital output, so normally it will not be logic to set its mechanical position from software. However, since there are sometimes cases (this is why the library also allow it) where the "practices beats the theory" one can do it ... but being aware of the possible logical problems that may occur from there.

Best regards, Mircea