Hi, Pete,
Unfortunately, I don't have a piece of code that does Analog Input and PWM output, however, the best way will be to have both operations within the same while loop. You could have something as AI -> PI -> PWM (first read the input, then process, then adjust the PWM). Or another method could be to implement pipelining, so multiple operations happen at the same time.
I hope this helps,
GValdes