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);
}