I'm working on a project that will use a high-speed rotary encoder attached to a DC Motor shaft which in turns drives a belted conveyor - sort of. I am using this library to record signals. I have a Leonardo, which according to specification has interrupt capability pins on 2,3 and I am using those two pins.
But I'm having serious problems. Using a digital vernier scale, and a second measurement with a ruler, I managed to compute a 250 impulse/mm. But when I drive the shaft by using a while loop like this:
Encoder myEncoder(2,3);
computedCounter = 50 * 250; //move about 50mm
void loop()
{
analogWrite(9,255); //send pwm
while(myEncoder.read() < computedCounter)
{
digitalWrite(8, HIGH); //enables direction pin
}
digitalWrite(8, LOW); //disable pin
analogWrite(9,0);
}
the reference moves more than 50. What's worse, the error is not systematic. If I send a lower pwm it gives me a larger error. And sometimes it goes under 50.
It's very confusing because I can't figure out what's going on. The mechanical system is quite stable. I've tried connecting 1k resistors to clean signals. It' the same.
My question is, how does Arduino deal with this kind of problem in general. Anyone that's worked with a similar project perhaps could share a thought?
It could be that my board is damaged, I've had this one for a while.