# Is this a good averaging method ?

**URL:** <https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194>\
**Category:** Programming\
**Created:** [August 15, 2020, 8:04am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194 "2020-08-15T08:04:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bogdan666](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@bogdan666](https://forum.arduino.cc/u/bogdan666)\
**Post date:** [August 15, 2020, 8:04am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/1 "2020-08-15T08:04:29Z")

</div>

I am trying to average out my voltmeter so my screen is somewhat stable, but all i see is a delay in response. is it ok ? or any other idea ?

```cpp
{for (int i = 0; i < 10; i++)
{
  V1 = ((ads.readADC_SingleEnded(0)) * 0.0001875) * 7;
  V2 = (((ads.readADC_SingleEnded(1)) * 0.0001875) - 0.588) * 20;
  V3 = (((ads.readADC_SingleEnded(2)) * 0.0001875) - 0.588) * 10;
  V4 = ((ads.readADC_SingleEnded(3)) * 0.0001875) * 8;
  delay(1);
}
{
  V1 = V1;
  V2 = V2;
  V3 = V3;
  V4 = V4;

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 15, 2020, 8:47am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/2 "2020-08-15T08:47:07Z")

</div>

> bogdan666:  
> is it ok ?

No

you are not averaging anything, just overwriting 10 times the same readings into the same variables and this is totally useless

```arduino
  V1 = V1;
  V2 = V2;
  V3 = V3;
  V4 = V4;

```

(the compiler will likely get rid of it)

an average is the sum of some data divided by the number of data elements

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [August 15, 2020, 9:01am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/3 "2020-08-15T09:01:00Z")

</div>

```cpp
V1 = 0;
V2 = 0;
V3 = 0;
V4 = 0;
for (int i = 0; i < 10; i++)
{
  V1 += ads.readADC_SingleEnded(0) * 0.0001875 * 7.0;
  V2 += (ads.readADC_SingleEnded(1) * 0.0001875 - 0.588) * 20;
  V3 += (ads.readADC_SingleEnded(2) * 0.0001875 - 0.588) * 10;
  V4 += ads.readADC_SingleEnded(3) * 0.0001875 * 8.0;
  delay(1);
}
V1 /= 10;
V2 /= 10;
V3 /= 10;
V4 /= 10;

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 15, 2020, 9:03am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/4 "2020-08-15T09:03:57Z")

</div>

I had hoped @bogdan666 would come back with some updated code of his own.

but hey, here you go full solution curtesy of @pcbbc 😉  
(just make sure to have the right type for V1, V2, V3 and V4)

an alternative is keeping the previous value and merging it with the new reading with some coefficient, for example 90% of the previous value plus 10% of the new reading if you want to smooth out changes or if you want fast reaction to change increase the %age for the new reading.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [August 15, 2020, 9:08am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/5 "2020-08-15T09:08:26Z")

</div>

Wouldn't it be easier to sum the raw readings, and do the conversion arithmetic on the averaged values?

---

<div class="post-metadata">

**Author:** ![Datman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/datman/32/219519_2.png) [@Datman](https://forum.arduino.cc/u/Datman)\
**Post date:** [August 15, 2020, 10:25am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/6 "2020-08-15T10:25:02Z")

</div>

Are you sure you really need it?...  
A RC at the input could be a simpler solution. You could also make a 2nd order filter, using a 10x resistor in the second filter.  
You also have to make sure that the ground circuit is correct, otherwise it adds noise to the reading.

---

<div class="post-metadata">

**Author:** ![bogdan666](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@bogdan666](https://forum.arduino.cc/u/bogdan666)\
**Post date:** [August 15, 2020, 1:32pm UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/7 "2020-08-15T13:32:05Z")

</div>

Thank you for the answers, the code works ok. As if i really need it, it looks like the simplest solution at the time.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 15, 2020, 1:55pm UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/8 "2020-08-15T13:55:29Z")

</div>

```cpp
unsigned int smooth(unsigned int newVal) {
  static unsigned int oldVal = 0;
  unsigned long sum;
    //  optimize sum = (oldVal * 3 + newVal) / 4;
    sum = ( (oldVal << 1) + oldVal + newVal) >> 2;
  }
  oldVal = sum;
  return oldVal;
}
...
  V1 = smooth( ads.readADC_SingleEnded(0) );
  V2 = smooth( ads.readADC_SingleEnded(1) );
...

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:42am UTC](https://forum.arduino.cc/t/is-this-a-good-averaging-method/670194/9 "2021-05-06T00:42:17Z")

</div>

Closed on May 6, 2021, 12:42am UTC


