# First Serial.print voltage is wrong, way too low. Ruins average calculation [closed]

The first Serial.print voltage reading is so far off it really screws up the moving average calculation. For example, the first reading is like 0.00014981, which is wrong. From the second line on it prints the correct voltage, like 0.00475721. I don't know where this first line is coming from. How can I keep that first line from being used in the average calculation? Here are links to the sketch and adc.

For the average calculation I added:

count = count + 1;

volt_mV = ((count-1)/count)*volt_mV + (1/count)*volt_V;

• Please post the exact code you are using, rather than letting us see what source you have started from and then try to guess what you have added or changed to it. – jfpoilpret Dec 4 '16 at 23:15

The code at the link is mediocre – it has dozens of unnecessary casts or declarations, dozens of superfluous statements, and numerous unused or unnecessary variables. Also, the code shown there has nothing about any averages, so evidently does not show whatever code you actually are running. You could revise your question and include the actual sketch within it.

Nevertheless, the code at the link looks like it will take a reading ok. Possibly your modifications, whatever they are, incorrectly initialize variables for the running average calculation, or incorrectly initialize some other variable not shown. Or there may be some hardware glitch that spoils the first reading.

Note, the formula you show for averaging – `((count-1)/count)*volt_mV + (1/count)*volt_V` – has a number of problems. First, if `count` is an integer, both of `((count-1)/count)` and `(1/count)` will compute to zero values. Secondly, as `count` becomes large, you will be adding a tiny value to a much larger value, which is ill-conditioned arithmetic. Third, although the formula computes a running average, it does not compute a moving average. That is, your formula computes the average of all readings taken so far, with all readings equally weighted. A moving average, on the other hand, typically will weight recent readings more heavily than old readings, for example via a window technique or via an exponential smoothing formula like `movingAverage = (15*movingAverage + newReading)/16`.
If it is truly only the first reading that is bogus, you could take one or a few readings during `setup()` whose values you ignore, before your program starts collecting data. Or you could investigate why your first reading is bogus....