Hi,
I'm trying to build my own mobile robot. Using the Arduino Mega. And I'm facing couple issues with reading the encoder.
So, I'm directly connecting the motor with 12V battery to have max rpm, and using "attachInterrupt()"
to read the encoder. (I'm attaching the code).
#define CHA 2
#define CHB 7volatile double master_count = 0; // universal count
volatile byte INTFLAG1 = 0; // interrupt status flag
volatile double rev_count = 0 ; //void setup() {
pinMode(CHA, INPUT);
pinMode(CHB, INPUT);
Serial.begin(9600);
Serial.println(master_count);attachInterrupt(digitalPinToInterrupt(CHA), flag, RISING);
// interrupt 0 digital pin 2 positive edge trigger
}void loop() {
if (INTFLAG1) {
//rev_count = master_count*(2*PI/9750);
//Serial.println(rev_count);
Serial.println(master_count);
INTFLAG1 = 0; // clear flag} // end if
} // end loopvoid flag() {
INTFLAG1 = 1;
// add 1 to count for CW
if (digitalRead(CHA) && !digitalRead(CHB)) {
master_count++;
}
// subtract 1 from count for CCW
if (digitalRead(CHA) && digitalRead(CHB)) {
master_count-- ;}
}
But, It cannot keep up the encoder count. I'm assuming that seen the motor shaft is rotating higher rpm it can not realize whether the pules has been changed or not.
If you know how to overcome this issue please help me out.
Thanks,
Here are the specs of my hardware (I'm also attaching pdf file):
Motor : Pittman GM8000 series (Gear ratio : 19.5:1, Voltage : 12 v , No Load rpm : 230).
Encoder : HEDS-9000/9100 ( 500 counts per revolution ).
motor_datasheet.pdf (119 KB)
Encoder_datasheet.pdf (343 KB)
count_print_2.ino (905 Bytes)