Hello everyone! Today we faced a new problem related to coders.Encoders 1, 2, 3 and 5 work fine, but encoders 4 and 6 do not work if they are connected to pins A with interrupts, namely to pins 19 and 21. When changing these pins to others (with interruptions), the encoders start working, which indicates that the motors and encoders themselves are in working order.
To check, I tested pin 19 as follows: when connecting a pin to 5 V, its status in the Serial Monitor changes, which indicates its operability. However, if you install INPUT_PULLUP, the pin stops working - although this configuration works fine on other pins. In addition, pin 21 does not respond in any way, which raises even more questions:
Code for checking the operability of pins:
const int buttonPin = 19;
int buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
delay(100);
}
Code for manual encoder performance check:
const int encoderPinA = 19;
const int encoderPinB = 24;
volatile long encoderPos = 0;
void setup() {
Serial.begin(115200);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
}
void loop() {
Serial.print("Encoder Position: ");
Serial.println(encoderPos);
delay(100);
}
void updateEncoder() {
int valueA = digitalRead(encoderPinA);
int valueB = digitalRead(encoderPinB);
if (valueA == HIGH) {
if (valueB == LOW) {
encoderPos++;
} else {
encoderPos--;
}
} else {
if (valueB == HIGH) {
encoderPos++;
} else {
encoderPos--;
}
}
}
My scheme:
Are there any ways to use other pins for encoder A in Arduino Mega? I saw people on the Internet who used Analog pins for the encoder, it won't be exactly for the encoder, but it didn't work in my code
I will be glad to have any advice that will help to understand this situation!
