When implementing the integral part, you can start by just adding or subtracting a constant value (start with one) and adding the integral part to the Proportional part. So, suppose you have a 5V analog potentiometer that is connected to an arm. The target or set point you want is 4V which corresponds to about 818 counts or units on the 10 bit A/D. If you are presently at 3V or about 614 counts or units, you want to move towards 818 counts. So, Error is 818 minus 614 = 204 counts. Multiply 204 by let's say 10 percent . To speed up calculation , use multiply by 26 and divide by 256 (26/256 is about 10 %).
So, (204 times 26)/256 is 20.7. With roundoff you will get 20 counts. Next, since PWM units are scaled 0 to 255, which is 1/4 range of 0-1023 a-d units, you will need to rescale (divide by 4) the 20 counts output by 4 to get 5 counts. This will be what you add to the present value you are already outputting to the PWM (which is scaled zero to 127 for one direction and 127 to 254 for the other direction). So if your present PWM output is 150 counts, you will add 5 counts for the proportional part of the 26.2 msec loop you are in. Next, since you are below your target, add 1 PWM output unit to the 150 counts for the integral portion of the PI gain. Remember this is all happening 38 times per second, so the numbers you add for the Proportional and Integral part can be small. Hope this sheds some insight into one way of making your PID work for you.
