Missing counting encoder

I have

  • DC motor with encoder 8000 pulse/round ( minertia series F B5L)
  • arduino ATmega328

I already use interupt but it still missing counting

int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;

void setup()
{
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}

void loop()
{
encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {
Serial.println (encoder0Pos, DEC);
valOld = valNew;
}

}

void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == LOW) {
encoder0Pos--;
}
else {
encoder0Pos++;
}
}
}
void StateB()
{
m = digitalRead(encoder0PinB);
}

This motor is 130 rpm (sorry not good in english ^^)

If it too fast for this code or arduino, how can I use this encoder?

Have any code for change this encoder too lower pulse?

Hi YeahYoo,

We need to know the answers to Richard's questions, ie rotational speed and position or just RPM needed.

However, bear in mind that interrupts are actually slower than properly-written polled code, so if the speeds are high you may need to adjust your code.


Rob

I try to do shaft position encoding.

PS : my project is balancing bicycle robot by stearing control
(This motor use to control stearing)