I am trying to read input from an optical rotary encoder that changes its state 360 times per rotation. My code currently reads the state from an arduino digital pin as fast as possible and counts when the state changes. This works while the motor spins slowly, but it misses a lot when the motor speeds up.
How can I detect changes in the state more accurately? Can I get an IC that would do this?
void setup() {
pinMode(12, INPUT);
Serial.begin(9600);
}
int count;
int last;
void loop() {
int b = digitalRead(12);
if (b != last){
count++;
Serial.println(count);
}
last = b;
}