Averaging Values in For Loop

I am interested in averaging a bunch of values together for a smoothing technique. I'm thinking a for loop? I know there is code out there already, however I am in part trying to write some custom code and also TRYING TO LEARN how FOR loops work (in their many iterations).

Anyway, specifically I'd like to do something like this:

  1. //gather and add all these values together
    if (value>0) {add all these values together}

  2. //find how many values from above there are to make an average
    if (value sensor value was taken then +1 until (for each time)

  3. //get the average
    (all values added together) + number of sensor values (step 2 from above)

  4. //If sensor value is zero (or negative number territory) than start fresh with new average
    (start over again)

The other part I'm not sure about is when it knows to check the sensor value. What if I want 10 samples....or 80 samples etc? Possibly we would need to implement a millis timer to know when to sample?

Thank you in advance. Again, I'm really trying to learn how this works so spelling it in noob level English would be greatly appreciated.

  1. //get the average
    (all values added together) + number of sensor values (step 2 from above)

Correction: 3. //get the average
(all values added together) ÷ number of sensor values (step 2 from above)

The other part I'm not sure about is when it knows to check the sensor value. What if I want 10 samples....or 80 samples etc? Possibly we would need to implement a millis timer to know when to sample?

If you want to spread the sampling over a period of time then don't use a for loop. A for loop, by its nature, locks you into it until all of its iterations have occurred. Actually, you can exit from it early but you can't then go back to where you left off.

Let the loop() function do what its name implies and use millis() for timing

Something like this maybe

averaging = false
doneMeasuring = false
counter = 0
total = 0

start of loop()
  code here to set averaging to true to start measurements when required
  if averaging equals true
    if wait between measurements has passed // use millis() for timing
      take a measurement
      add measurement to the total
      increment counter
        if all measurments taken
          set averaging to false to prevent more being taken
          set doneMeasuring to true to trigger calculation
        end if
    end if

    if doneMeasuring is true
      divide total by counter to get average of measurements
      set doneMeasuring to false to prevent more calculations until required
    end if
end of loop

Woops...yes divide by... thanks.

Ok that explaination makes sense to me. I will try coding it tonight and try to implement on real system on Monday. Thanks UK

You cold look into the code of TPinInput from the TDuino library, which can bot calculate a buffered average or a non-buffered average - bot non-blocking :slight_smile:

UsernameD:
Ok that explaination makes sense to me. I will try coding it tonight and try to implement on real system on Monday. Thanks UK

I suggest that you start with a small number of data points read from an array of values. That way you have control over what goes in and can determine whether the calculated average is what you expect it to be.