I'm currently using the delay function to run a setpoint calculation function ever 2ms (returns theoretical motor position). This result is then subjected to a PID library, the result of which is outputted on Pin 9 using analogWrite() as a PWM signal.
As the delay() function doesn't allow the CPU to perform other tasks (most importantly, processing encoder interrupts), the resulting motion is unpredictable (smoothness dependant on accel and max_vel parameters).
My initial idea was to use timer interrupts (Arduino Playground - Timer1) to set a flag every 2ms. However, in many cases the motors emit an odd buzzing noise (usually increasing in pitch) or the occasional erratic movement.
I've read that the above class (or maybe Timer in general?) breaks analogwrite() for pin 9 and 10.
As the bot I'm using is hardwired (and I don't want to even attempt to find an electrical workaround. i.e. rewiring to different pins, etc), I have no choice but to use pin 9 and 10 for my motor outputs.
Can anyone figure out a software solution to this problem?