Hi,
I have a question about how to read encoder data using interrupt function in arduion.
I am using the encoder from below link.
Pololu - 250:1 Micro Metal Gearmotor HP 6V with 12 CPR Encoder, Back Connector.
So it has outputA and ouputB with 12 CPR.
I am using leonardo arduino board which I know it has 5 interrupt pin.
each pin is 0, 1,2,3,7 and I am using 0, 1,2,3 pin.
Also as I know each pin is match with interrupt pin 2,3,1,0 from Leonardo pinout.
So I setup the below arduino code to read the position data. Currently I am using two motor. So I need two position data. But I have a problem while reading the data.
As I know, 12 CPR means that I can discrimate 12 signals from one rotation. So with the below code 'm1_pos, m2_pos' values should be increase in 12/rotation. But actually it doesn't, it sometimes 180 or sometime 5000. (What a ridiculous number).
So I have a question the cause of my issues. Maybe my code is wrong or interrupt setting is wrong or else. I am directly wiring encoder each output pin of encoder to leonardo pinout.
(I skip some code from below code which is not related with encoder reading)
Thank You for any advice!
int m1_e1 = 0; //
int m1_e2 = 1; //
int m2_e1 = 2; //
int m2_e2 = 3; //
void setup() {
Serial.begin(115200);
//motor 1 interrupt
attachInterrupt(2, do_m1e1, CHANGE);
attachInterrupt(3, do_m1e2, CHANGE);
//motor 2interrupt
attachInterrupt(1, do_m2e1, CHANGE);
attachInterrupt(0, do_m2e2, CHANGE);
pinMode(m1_en, OUTPUT);
pinMode(m2_en, OUTPUT);
pinMode(m1_in1, OUTPUT);
pinMode(m1_in2, OUTPUT);
pinMode(m2_in1, OUTPUT);
pinMode(m2_in2, OUTPUT);
pinMode(m1_e1, INPUT);
pinMode(m1_e2, INPUT);
pinMode(m2_e1, INPUT);
pinMode(m2_e2, INPUT);
}
void do_m1e1(){
// look for a low-to-high on channel A
if (digitalRead(m1_e1) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(m1_e2) == LOW) {
m1_pos = m1_pos + 1;
}
else {
m1_pos = m1_pos - 1;
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(m1_e2) == HIGH) {
m1_pos = m1_pos + 1;
}
else {
m1_pos = m1_pos - 1;
}
}
//Serial.println (m1_pos);
m1_update = true;
}
void do_m1e2(){
m1_update = true;
// look for a low-to-high on channel B
if (digitalRead(m1_e2) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(m1_e1) == HIGH) {
m1_pos = m1_pos + 1;
}
else {
m1_pos = m1_pos - 1;
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(m1_e1) == LOW) {
m1_pos = m1_pos + 1;
}
else {
m1_pos = m1_pos - 1;
}
}
}
void do_m2e1(){
m2_update = true;
// look for a low-to-high on channel A
if (digitalRead(m2_e1) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(m2_e2) == LOW) {
m2_pos = m2_pos + 1;
}
else {
m2_pos = m2_pos - 1;
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(m2_e2) == HIGH) {
m2_pos = m2_pos + 1;
}
else {
m2_pos = m2_pos - 1;
}
}
}
void do_m2e2(){
m2_update = true;
// look for a low-to-high on channel B
if (digitalRead(m2_e2) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(m2_e1) == HIGH) {
m2_pos = m2_pos + 1;
}
else {
m2_pos = m2_pos - 1;
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(m2_e1) == LOW) {
m2_pos = m2_pos + 1;
}
else {
m2_pos = m2_pos - 1;
}
}
}