Hi I am new with using encoder. I have difficult to get the Magnetic Encoder Pair Kit read correct count by using pololu encoder function such as encoders_get_counts_m1(). It is keep return 0 to me. Below is my code please point out where is my mistake? The reason I am using encoder is I want to make both motor turn in same speed then can make the robot drive straight. It is great to have someone give me some code example for pololu encoder. thanks
#include <PololuWheelEncoders.h>
#include <ATX2.h>
#define PololuWheelEncoders encoders
void setup() {
// put your setup code here, to run once:
XIO();
OK();
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
void loop() {
// put your main code here, to run repeatedly:
encoders_init(24, 26, 28, 30);
Serial.print(encoders_get_counts_m1());
Serial.print(" ");
Serial.println(encoders_get_counts_m2());
}
but I also tried another encoder library, it returns correct increment count while I turn my motor forward manually. But when I turn motors automatic it doesn’t return correct value again.Below is my code
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <ATX2.h>
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEncL(28, 30);
Encoder myEncR(24, 26);
// avoid using pins with LEDs attached
void setup() {
XIO();
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
myEncL.write(0);
myEncR.write(0);
}
long oldPositionL = -999;
long oldPositionR = -999;
void loop() {
motor(34, 2);
motor(56, 2);
long newPositionL = myEncL.read();
long newPositionR = myEncR.read();
if (newPositionL != oldPositionL || newPositionR != oldPositionR) {
oldPositionL = newPositionL;
oldPositionR = newPositionR;
Serial.print(newPositionL);
Serial.print(" ");
Serial.println(newPositionR);
}
}