0

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.

https://github.com/Protocentral/ProtoCentral_ads1262/tree/master/Libraries http://www.ti.com/lit/ds/symlink/ads1262.pdf

For the average calculation I added:

count = count + 1;

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

1
  • 4
    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
3

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.

Several fixes are possible. You could discard the first reading. You could discard the first reading of each set of readings. You could take readings in sets of three or five and only keep the median reading. You could take sets of readings and discard the highest and lowest readings. You could apply exponential smoothing or some other form of smoothing to the readings.

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.

1
  • Your movingAverage formula is a lot better than the one I was using. It quickly fades out the error from the first reading. Its nice to learn the difference between a running average vs. a moving average. – Rico Dec 5 '16 at 0:23
0

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....

-2

The first Serial.print voltage is CORRECT with all others afterwards being wrong so the thing to do is USE the first one and ignore all other!

Not the answer you're looking for? Browse other questions tagged or ask your own question.