Rotary_Encoder_Breakout-Illuminated icon indicating copy to clipboard operation
Rotary_Encoder_Breakout-Illuminated copied to clipboard

Rotary encoder demo code

Open NGL3324 opened this issue 5 years ago • 1 comments

  1. Code uses syntax: attachInterrupt(1, rotaryIRQ, CHANGE);

from https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ this doesn't necessarily work for different boards:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended) attachInterrupt(interrupt, ISR, mode) (not recommended) attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

  1. Code uses: attachInterrupt(1, rotaryIRQ, CHANGE);

although the code works as written (??), Pin 1 is not used in the sketch. The rotary encoder is wired as: (// Rotary encoder pin A to digital pin 3* * Pins marked with an asterisk should not change // Rotary encoder pin B to analog pin 3 // Rotary encoder pin C to ground

...so how this works attaching the interrupt to the wrong pin, I dunno.

  1. Code states to attach LEDs directly to GPIO ports:

// Rotary encoder pin 1 (red anode) to digital pin 10 // Rotary encoder pin 2 (green anode) to analog pin 1 // Rotary encoder pin 3 (button) to digital pin 4

they should be attached through series dropping resistors to avoid high intensity one-shot flashes.

  1. Code sets input pullups using deprecated syntax used prior to Arduino 1.0.1:

digitalWrite(ROT_A, HIGH); // turn on weak pullup

this should be replaced with pinMode(pin, INPUT_PULLUP); where pin is ROT_A, ROT_B

NGL3324 avatar Jan 28 '20 07:01 NGL3324

  1. Code uses syntax: attachInterrupt(1, rotaryIRQ, CHANGE);

from https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ this doesn't necessarily work for different boards:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended) attachInterrupt(interrupt, ISR, mode) (not recommended) attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

  1. Code uses: attachInterrupt(1, rotaryIRQ, CHANGE);

although the code works as written (??), Pin 1 is not used in the sketch. The rotary encoder is wired as: (// Rotary encoder pin A to digital pin 3* * Pins marked with an asterisk should not change // Rotary encoder pin B to analog pin 3 // Rotary encoder pin C to ground

...so how this works attaching the interrupt to the wrong pin, I dunno.

Both issues are related. I agree about 1, this code is pretty old and still uses the old Arduino convention.

About 2. and why this code works, you find the answer in your own link:

However, older sketches often have direct interrupt numbers. Often number 0 (for digital pin 2) or number 1 (for digital pin 3) were used.

  1. Code sets input pullups using deprecated syntax used prior to Arduino 1.0.1:

digitalWrite(ROT_A, HIGH); // turn on weak pullup

this should be replaced with pinMode(pin, INPUT_PULLUP); where pin is ROT_A, ROT_B

Agree. This code could be updated.

iceananas avatar Feb 21 '20 11:02 iceananas