Open NGL3324 opened 4 years ago
- 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.)
- 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.
- 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.
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.)
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.
// 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.
digitalWrite(ROT_A, HIGH); // turn on weak pullup
this should be replaced with pinMode(pin, INPUT_PULLUP); where pin is ROT_A, ROT_B