I am trying to read the data off of two US Digital encoders. These encoders are attached to two motors with back shafts on a large robot that I am planning to enter into the Robo-Magellan. The data comes in properly for the left encoder, but not the right one. Meaning, no data comes in from the right encoder when I turn it. Also, the data for the right encoder is almost, but not quite, the same as that for the left encoder (the one I am turning). The right encoder should not be communicating with the left encoder, instead it should show its own data in the serial window. In the case when you turn the left wheel there should be nothing showing for the right wheel.
I have been working on this for hours, so I am hoping you guys can help me.
Thanks!
Here is a picture of my robot (at the moment):
Here is the link to the encoder's product page:
Here is my code:
volatile long RightDistance = 0;
volatile long LeftDistance = 0;
long PreviousLeftDistance = 0;
long PreviousRightDistance = 0;
// connect right encoder wire A to pin 2
#define RightEncoderPinA 18
// connect left encoder wire A to pin 3
#define LeftEncoderPinA 19
// connect right encoder wire B to pin 13
#define RightEncoderPinB 50
//connect left encoder wire B to pin 14
#define LeftEncoderPinB 51
#define RightEncoderInterupt 5
#define LeftEncoderInterupt 4
void setup()
{
Serial.begin(9600); // open the serial port at 9600 bps
attachInterrupt(RightEncoderInterupt, RightEncoderISR, CHANGE); // connect encoder wire A to pin 5
attachInterrupt(LeftEncoderInterupt, LeftEncoderISR, CHANGE); // connect encoder wire A to pin 4
pinMode(RightEncoderPinA, INPUT); // set the pin to input
pinMode(RightEncoderPinB, INPUT); // set the pin to input
pinMode(LeftEncoderPinA, INPUT); // set the pin to input
pinMode(LeftEncoderPinB, INPUT); // set the pin to input
}
void loop()
{
if ((RightDistance != PreviousRightDistance) | (LeftDistance != PreviousLeftDistance))
{
Serial.print(" Right ");
Serial.print(RightDistance);
Serial.print(" Left ");
Serial.print(LeftDistance);
Serial.println("");
//delay(100);
}
PreviousLeftDistance = LeftDistance;
PreviousRightDistance = RightDistance;
}
void RightEncoderISR()
{
if (RightEncoderPinA == RightEncoderPinB)
{
RightDistance--;
}
else
{
RightDistance++;
}
}
void LeftEncoderISR()
{
if (LeftEncoderPinA == LeftEncoderPinB)
{
LeftDistance--;
}
else
{
LeftDistance++;
}
}
// connect right encoder wire A to pin 2 #define RightEncoderPinA 2
// connect left encoder wire A to pin 3 #define LeftEncoderPinA 3
// connect right encoder wire B to pin 13 #define RightEncoderPinB 13
//connect left encoder wire B to pin 14 #define LeftEncoderPinB 14
I am using is the Arduino Mega which has the two A pins on the encoders are connected to pins digital pins 18 and 19, and the two B pins on the encoders are connected to digital pins 50 and 51.
Here is the code again, but with the comments updated (the color might make it easier to read):
volatile long RightDistance = 0;
volatile long LeftDistance = 0;
long PreviousLeftDistance = 0;
long PreviousRightDistance = 0;
#define RightEncoderPinA 18 // connect right encoder wire A to pin 2 #define LeftEncoderPinA 19 // connect left encoder wire A to pin 3 #define RightEncoderPinB 50 // connect right encoder wire B to pin 13 #define LeftEncoderPinB 51 //connect left encoder wire B to pin 14
#define RightEncoderInterupt 5 //the interupt number for the right encoder #define LeftEncoderInterupt 4 //the interupt number for the left encoder
void setup()
{ Serial.begin(9600); // open the serial port at 9600 bps:
attachInterrupt(RightEncoderInterupt, RightEncoderISR, CHANGE); // connect encoder wire A to pin 5 (RightEncoderInterupt)
attachInterrupt(LeftEncoderInterupt, LeftEncoderISR, CHANGE); // connect encoder wire A to pin 4 (LeftEnocderInterupt)
pinMode(RightEncoderPinA, INPUT); // set the RightEncoderPinA to input
pinMode(RightEncoderPinB, INPUT); // set the RightEncoderPinB to input
pinMode(LeftEncoderPinA, INPUT); // set the LeftEncoderPinA to input
pinMode(LeftEncoderPinB, INPUT); // set the LeftEncoderPinB to input
}
void loop()
{
if ((RightDistance != PreviousRightDistance) | (LeftDistance != PreviousLeftDistance)) //this makes the distance print only when it changes
{ Serial.print(" Right "); Serial.print(RightDistance); //print the RightDistance Serial.print(" Left "); Serial.print(LeftDistance); //print the RightDistance Serial.println("");
}
I too at first thought you had the interrupt numbers/pin numbers wrong, but after some references I think you do have that correct.
Why don't don't you just swap the two channel A wires and see if the problem moves. If it stays the same you probably have something wrong in the code, if the problem moves then it's most likely wiring/external component problem.