I'm trying to control a single joint of a robotic arm using arduino nano. at first I was using this library in order to read the encoder, however since it polls the encoder at regular intervals I was missing out some encoder readings depending on the speed of rotation at the time. In order to solve this I opted to reading the encoder based on interrupts, using this code. Now it looks like I stopped missing encoder readings, however my PID which was working well (I didn't get around to tuning it to final values yet, but generally it was working as expected) started going haywire, it will start by providing a correct output, but then without even getting close to it's setpoint change direction and fluctuate around some random(?) point, giving a few outputs of +255 and then a few of -255 (output range is 255 - (-255) ). I'm suspecting that the rather high number of interrupts introduced by the encoder is somehow messing with the PID timer or proccess. I've tried pre-scaling the value between reading it and feeding it to the PID, with no results. by printing to serial, I can confirm the encoder output looks fine and it's the PID that's giving out wierd values.
Also, now that I have gazillions of interrupts from the encoder, I'm missing some I2C communications, if someone have any idea on how to make them co-exist I would appreciate the advice.
I'm considering using an Attiny to read the encoders directly and pre-scale the value before feeding it to the Arduino (the encoder is mounted on the motor shaft and there is a ~400:1 gear, so I can afford pre-scaling the reading), but would rather KISS...