Conceptual Question for averaging multiple sensors at the same time - newbie

Hi All

I have been researching how to take multiple sensor values and average them, there is a lot of examples out there. I am building a Spa Pool controller with a water and air temp sensors plus an LDR (used to detect if the pool lights should be on while you leave the spa area). currently I’m using LM35 for the temp sensors while building which can have the odd erroneous values. I intend to use a trimmed averaging model for averaging values (drop highest and lowest and average the rest) and i found this code below which I think works well in theory.

Question is can I run this code on the 3 sensors at the same time without having to have variables for air, water and light. I was going to average the values over 1 minute with 10 values and average the 8.
Or do I run the function and store the results in an array for “Air” “water” and “light” then do the math on the array.

The reason I ask is the topic I was reading was refer to “callback” functions, a topic well above my pay grade, but is this a way to use a piece of code in multiple places. or am I’m I way off ?

Also I’m new to programming, it has been my 6 week lock-down leaning mission from “blink” to 1500 lines of code so far, so be kind.

int16 olympic_ten(void) {
    int16 imax=0;
    int16 imin=65535;
    int16 isum=0;
    int16 itemp;
    int8 ctr;

    for (ctr=10;ctr>0;ctr--) {
        if (itemp<imin) imin=itemp;
        if (itemp>imax) imax=itemp;
    //Now we have the sum of ten readings, and the max  & min readings
    isum-=imax; //subtract the min and max from the sum, now only 8 are left

    itemp=isum/8; //compiler will automatically optimize this to a shift
    return itemp;

If you intend to drop the highest and lowest values in each set of readings then you need to store the 10 values for each sensor in separate arrays or perhaps a single array of structs. Then, when the period is over, sum the 10 values, determine the highest and lowest of each value, subtract them from the totals and divide the result by the remaining number of values

There is no obvious reason why you cannot sample the 3 sensors at the end of each sub period and save the values to their own array or the struct as long as none of the code blocks the free running of the program

Forget about callback functions and do it the simple way

Where is int16 defined? Is it signed or unsigned?
It’s an odd type name for unsigned type.

Really you need to address why you are getting hugely erroneous values.
Something is wrong elsewhere and you are just applying a sticking plaster.

Read all about Arduino data types here:

thanks @UKHeliBob

I thought that was probably the case, I tried to get my head around call backs but it wasn't sinking in, maybe in time. I work with numbers in excel (statistical analysis) and i would love to continuously test the vales (i'e every 6 seconds drop the last value and test the next 10, dropping highest and lowest and averaging the eight, so 1-10, test 2-8 then 2-11 test 3-9 etc) currently that is beyond my ability programming wise in c++ but I can work with 1-10 then 11-20 etc for what it is doing the results will be fine at this stage.

i probably didn't explain well enough, the code is directly copied from another post but I like the approach to the math.

I specifically don't want numbers jumping around on the display so i think averaging is the best approach. In the background I don't mind if they do move. It remains to be seen how good the water proof sensors I have ordered are but I only see them being tested every minute there is no need to have them that reactive with 3000 ltrs of water, and on the LCD I don't want the values to jump due a strange erroneous value (maybe it is just the interference on the breadboard on my desk)

I have read various articles about smoothing via capacitors etc but I think I can achieve it with simple statistical math rather than adding in more components to the system i would rather get the raw numbers and work with them.


This process of getting ten samples and dropping two seems a bit complex for the purpose. I’d be more inclined to go with a moving average so there’s no need to keep multiple readings.

if you just want to

I’d be more inclined to go with a moving average so there’s no need to keep multiple readings.

a moving average (leaky integration)

A += (sample - A) * K K < 1

periodically (every 10 seconds?) read a sample, update the average and take some action.

Thanks gcjr

That worked perfectly.