I am trying to use two encoders on one Arduino Mega 2560, on encoder for each wheel.
The first channel using Pin A=2 and Pin B=4, works great no issues.
The second channel using Pin A=3 and Pin B=4, the interrupt comes thru but it will only count in one direction).
If I put the encode for the second channel on the first channel, it works fine.
Any ideas?
Here is the code:
// Rotary Encoder Inputs
//Pin 2 and 3 can have external interrupts on them
//Pin 18 and 19 can also have external interrupts
int Encoder1_PinA = 2;
int Encoder1_PinB = 4;
int Encoder1_counter_2 = 0;
volatile int Encoder1_counter = 0;
int Encoder2_PinA = 3;
int Encoder2_PinB = 5;
int Encoder2_counter_2 = 0;
volatile int Encoder2_counter = 0;
volatile int Encode1_PinB_Value =0;
volatile int Encode2_PinB_Value =0;
char in_char;
unsigned long my_time;
String send, send2, info, info2, tab = "\t";
unsigned long start, stop, elapsed;
int Encoder1_pin_a = Encoder1_PinA, Encoder1_pin_b = Encoder1_PinB;
int Encoder2_pin_a = Encoder2_PinA, Encoder2_pin_b = Encoder2_PinB;
void setup()
{
pinMode(Encoder1_PinA, INPUT);
pinMode(Encoder1_PinB, INPUT);
pinMode(Encoder2_PinA, INPUT);
pinMode(Encoder2_PinB, INPUT);//INPUT_PULLUP
//PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
Serial.begin(115200);
start = millis();
attachInterrupt(digitalPinToInterrupt(Encoder1_pin_a), Encoder1_change, RISING);
attachInterrupt(digitalPinToInterrupt(Encoder2_pin_a), Encoder2_change, RISING);
Serial.println("Ready to go");
}
void Encoder1_change()
{
Encode1_PinB_Value =digitalRead(Encoder1_pin_b);
if (Encode1_PinB_Value == HIGH)
{
Encoder1_counter = Encoder1_counter - 1;
}
else
{
Encoder1_counter = Encoder1_counter + 1;
}
}
void Encoder2_change()
{
Encode2_PinB_Value =digitalRead(Encoder2_pin_b);
if (Encode2_PinB_Value == HIGH)
{
Encoder2_counter = Encoder2_counter - 1;
}
else
{
Encoder2_counter = Encoder2_counter + 1;
}
}
void DisplayPins()
{
Serial.print(" Encoder1[");
if (digitalRead(Encoder1_pin_a) == HIGH)
{
Serial.print("PinA=High ");
}
else
{
Serial.print("PinA=Low ");
}
if (digitalRead(Encoder1_pin_b) == HIGH)
{
Serial.print(" PinB=High ");
}
else
{
Serial.print(" PinB=Low ");
}
Serial.print(" PinB_Value="); Serial.print(Encode1_PinB_Value, DEC);
Serial.print("] ");
Serial.print(" Encoder2[");
if (digitalRead(Encoder2_pin_a) == HIGH)
{
Serial.print("PinA=High ");
}
else
{
Serial.print("PinA=Low ");
}
if (digitalRead(Encoder2_pin_b) == HIGH)
{
Serial.print("PinB=High ");
}
else
{
Serial.print("PinB=Low ");
}
Serial.print(" PinB_Value="); Serial.print(Encode2_PinB_Value, DEC);
Serial.println("]");
}
void loop()
{
if (Encoder1_counter != Encoder1_counter_2)
{
noInterrupts();
info = (String)Encoder1_counter;
Encoder1_counter_2 = Encoder1_counter;
interrupts();
send = info;
Serial.print("1-->");
Serial.print(send);
DisplayPins();
}
if (Encoder2_counter != Encoder2_counter_2)
{
noInterrupts();
info = (String)Encoder1_counter;
Encoder1_counter_2 = Encoder1_counter;
info2 = (String)Encoder2_counter;
Encoder2_counter_2 = Encoder2_counter;
interrupts();
send = info;
send2 =info2;
Serial.print("2-->");
Serial.print(" [1]-->");
Serial.print(send);
Serial.print(" [2]-->");
Serial.print(send2);
DisplayPins();
}
while (Serial.available() > 0)
{
char ch = Serial.read();
Serial.print(ch);
if(ch =='r')
{
Serial.println("Reset encoder");
noInterrupts();
Encoder1_counter=0;
Encoder1_counter_2= 0;
interrupts();
}
if(ch == 'i')
{
DisplayPins();
}
}
}