Hi,
It looks like one problem is that you can not take the average over any period you feel like taking it over, you must think about what it is that you are measuring, such as the frequency. It doesnt matter if you take 10 samples or 100 samples that's not the only consideration.
For example, sample a 10vac, 1 Hz signal with 10 samples near the peak and you'll get values near 14 volts, but sample near the zero crossing and you could get zero.
What this means is that the samples have to be spread depending on the lowest frequency. If the lowest is 100Hz then you should be sampling maybe 10 times per cycle over maybe a 1 second period. We could look at some exact numerical examples to see how this works better though. If you take too few samples over some time near one cycle but not exactly one cycle, that probably wont work either because when we take the average we have to know the period and that implies that we must be synchronized to the waveform period. Over many cycles however, the error damps out.
There is a nicer, cleaner way to do it however, and that is to digitally filter the input using methods from sampling theory. The simplest of outcomes from this would be a filter that ends up being a running average filter, which could also be called a digital low pass filter.
The implementation in code is almost TOO easy.
Starting with what we know about averages, if we had 10 samples that averaged 20v, then we know that if we did a true running average we would subtract the first sample taken and then add in the last sample taken, then compute the average again. But since we already have the information about the average (the average itself) we can approximate this process by subtracting instead 1/N times the average, which in this case would be 2v. We then add in the last sample divided by N, and we have the new average ... all without any extra storage. In equation code form this looks like this:
Avg=((N-1)*Avg+NewSample)/N
and that would most likely have to be done in floats.
So for example if we choose N=32 then we would have:
Avg=((31*Avg)+NewSample)/32
however, N should be large enough to get a true average over enough cycles of the lowest frequency. This might mean making N much larger than 32. Of course you then have to watch the precision and make sure there is no overflow, and also make sure the entire progression is fast enough for your needs. If it turns out to be too slow then you may have to go to a more complex calculation.
Since you are doing DC plus a sine wave, if you are sure it is a sine wave then you might be able to search for the peak and valley and simplify the calculation. We could look at this more if you like.
You could also convert the equation above to other forms if you like.