Hi noneyabussiness
there is a lot of noise on the ADC input from the AC output. I think I could not have made the feedback wiring more prone to picking up noise if I tried. I have about 2 feet of twisted pair wires coiled up and passing by the mosfet bridge board which convey the DC low voltage sample of the AC output to the ADC input pin. I want a bit of noise in fact so it will force me to deal with it in a competent manner.
First I put the DC volts through a voltage divider to give me about 3.5V, then straight into a LP filter (RC). Then the 2 feet of antenna (or cable if you like), then another RC LP filter only 1 inch before the arduino ADC input pin.
This presents a relatively low noise signal ready for sampling.
Initially I only used the first LP filter near the divider, so as to see how much noise I could deal with via digital filtering.
It seems it could handle a lot and remain stable.
The second filter was included because I wanted to see how clean I could get it if I placed it as close as possible to the ADC input.
This has not changed the PID stability much if at all.
The current version of the code without debug printing samples this signal at about 2.8kHz and then applies a moving average filter to the result.
eg
ch0 = tc * (float)analogRead(0)/1024.0 + (1.0 - tc) * ch0;
where tc is the time constant of the filter and is set to 0.01 in my code and ch0 is the filtered result of sampling ADC channel 0
(After removing the debug print code, the PID became unstable immediately! This didn't break any mosfets - yet. A little
less proportional gain and the PID came good.)
All of these filters have a relatively poor stop band attenuation slope, but since this is a prototype device designed for my education I can live with it's many naive design limitations since these give me so much to deal with and learn from.
There are better digital filters, where "better" means sharper attenuation slopes, more attenuation etc.
My current favorite is the bi-quad Butterworth.
see
http://www.earlevel.com/main/2013/10/13/biquad-calculator-v2/and
http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/for some well written explanations and an online toy to play filter designer.