Averaging Array

I'm starting a new project in which I envision collecting an array of data sampled every few seconds from a pressure sensor
I wish to obtain the AVERAGE value over time for display on a bar graph ..... the sum of the array values divided by count of values stored
This value being updated perhaps every 5 minutes.

I don't see why not ... make the array update continually on a FILO first in last out basis

thus eliminating most of the "noisy - pressure spikes" supplied to sensor.

Is there a function call for summing array values or should Average Function simply loop thru array to obtain value and divide by count.

Any pointers greatly appreciated!

Tank Level Project.pdf (108 KB)

Any pointers greatly appreciated!

A circular array is what you need. Increment the index until you reach the end, then start over at 0. You know how many elements are in the array, so computing the average is easy - whether the array is partially full or completely full.

check these

If you are just looking for the average value every five minutes (ie, not a running average of the last five minutes), you can do that without an array.

You need 2 variables - the current average and the number of values read so far.

  1. Initialise the average to the first value in the time period, set the count = 1
  2. Every other value that arrives is added as follows: average = ((old_average * count) + current_value)/(count+1);
  3. count = count+1

At the end of your period use the avarage, reset the counters and start again.

Running average over the last five minutes is a different story and will need some form of storage.