Smoothing multi sensors for 16 analog inputs

Hi this is currently my first post so i dont know if i am doing this right.
This is the code i am working on it is for testing the voltage of 16 different analog inputs.

const int inputpins[16] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15};                    // define the pins being measured
int pin_count = sizeof(inputpins)/sizeof(inputpins[0]); // Count the number of pins

const int numReadings = 5;   // value to determine the size of the readings array.
const int numChannels = 16;
int readings[numChannels][numReadings];      // the readings from the analog input
int index;              // the index of the current reading
int total [numChannels];               // the running total
int average [numChannels];             // the average

float Vcc = 5.01;               // Variable to match the analogReference, this is added mostly for design

void setup() {
  analogReference(DEFAULT);
//  analogReference(INTERNAL2V56);                                       // Use interal 2.56v ref

  Serial.begin(9600);
  for (int pin = 0; pin < pin_count; pin++){
    Serial.print(" | ");
    Serial.print("A");
    Serial.print(inputpins[pin] - A0);
    Serial.print(" | ");
  }
Serial.println();
}

void loop() {
  String values;
  for (int pin = 0; pin < pin_count;pin++){                                    
    float V = analogRead(inputpins[pin])*(Vcc/1023.0);    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    values += "|";
    values += V;
    values += "| ";                                     // append the readings to the string of values followed by a "|"
  }

  for (int chan = 0; chan < numChannels; ++chan ) {
    total[chan] = total[chan] - readings[chan][index]; 
  for (int pin = 0; pin < pin_count;pin++){                                    
    total[pin] += analogRead(inputpins[pin])*(Vcc/1023.0);    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    total[chan] = total[pin];
  }
  //   total[chan] +=analogRead(inputpins[chan])*(Vcc/1023.0);
  //  total[chan] = total[chan] + readings[chan][index];
    index = index + 1; // advance to the next position in the array
    if (index >= numReadings){ // if we’re at the end of the array
      index = 0; //wrap around to the beginning
    }
    average[chan] = total[chan] / 5; //Averages the readings
}

  for (int pin = 0; pin < pin_count; pin++){
    Serial.print(" | ");
    Serial.print("A");
    Serial.print(inputpins[pin] - A0);
    Serial.print(" | ");
  }
    Serial.println();
  delay(1);             // delay in between reads for stability
    Serial.print(values);                        // send values to serial
    Serial.println();
  delay(500);           // delay in between reads for stability

  for (int pin = 0; pin < pin_count; pin++){
    Serial.print(" Sum:A");
    Serial.print(inputpins[pin] - A0);
  }
    Serial.println();

    for (int chan = 0; chan < numChannels; ++chan ) {
      Serial.print(" | ");
      Serial.print(average[chan]);
      Serial.print(" | ");
    }
      Serial.println();
      delay(1);        // delay in between reads for stability
}

The issue i am having is with:

  for (int chan = 0; chan < numChannels; ++chan ) {
    total[chan] = total[chan] - readings[chan][index]; 
  for (int pin = 0; pin < pin_count;pin++){                                    
    total[pin] += analogRead(inputpins[pin])*(Vcc/1023.0);    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    total[chan] = total[pin];
  }
  //   total[chan] +=analogRead(inputpins[chan])*(Vcc/1023.0);
  //  total[chan] = total[chan] + readings[chan][index];
    index = index + 1; // advance to the next position in the array
    if (index >= numReadings){ // if we’re at the end of the array
      index = 0; //wrap around to the beginning
    }
    average[chan] = total[chan] / 5; //Averages the readings
}

I have looked at the smoothing example from arduino but the total[chan] just keeps growing therefore i think that the - readings[chan][index] but i am having a hard time figuring it out. Can anyone tell me what i am doing wrong ?

On the serial monitor sum keeps growing:

|1.18| |1.27| |1.25| |1.23| |1.16| |1.11| |1.11| |1.07| |1.05| |1.01| |1.01| |0.97| |0.99| |0.95| |0.96| |0.94|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 6 | | 6 | | 6 | | 6 | | 6 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 | | 7 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.48| |1.58| |1.56| |1.54| |1.47| |1.42| |1.40| |1.31| |1.26| |1.24| |1.24| |1.21| |1.22| |1.17| |1.16| |1.07|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 7 | | 7 | | 7 | | 7 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 | | 8 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.58| |1.69| |1.71| |1.72| |1.68| |1.67| |1.68| |1.63| |1.64| |1.65| |1.67| |1.63| |1.65| |1.58| |1.56| |1.43|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 9 | | 10 | | 10 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|1.57| |1.68| |1.69| |1.70| |1.67| |1.66| |1.68| |1.67| |1.71| |1.72| |1.74| |1.72| |1.74| |1.68| |1.66| |1.53|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 10 | | 10 | | 10 | | 10 | | 10 | | 10 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 12 |

When having problems with code, loops can scramble your brain. I much prefer just brute-forcing in the code prototyping stage.

This function example does a roll & average across 5 samples.

#include <Streaming.h>                       // library can be found here: http://arduiniana.org/libraries/streaming/
#define DIAG false // or true

const int Average_Depth = 5 ;                // can be changed at expense of loop{} total time
int AnalogAverageMatrix [9][Average_Depth];  // 9 is based on A0, A1, A2, A3, A4, A10, A12, A13, A15

int analogAverage ( int ROW, int ApinNo )
{
    int ReadVal = 0;
    // Rollup ... small matrix, just assign for testing, could be a for() if desired
    // Oldest value is [0], newest value [4]
    AnalogAverageMatrix [ROW][0] = AnalogAverageMatrix [ROW][1];
    AnalogAverageMatrix [ROW][1] = AnalogAverageMatrix [ROW][2];
    AnalogAverageMatrix [ROW][2] = AnalogAverageMatrix [ROW][3];
    AnalogAverageMatrix [ROW][3] = AnalogAverageMatrix [ROW][4];
    // do two reads ... as a test, but one should really be enough IMO
    ReadVal = analogRead( ApinNo ); // delayMicroseconds( 10 );
    AnalogAverageMatrix [ROW][4] = analogRead( ApinNo );
    if ( DIAG) Serial << ROW  << ": " << AnalogAverageMatrix [ROW][0] << ", " <<  AnalogAverageMatrix [ROW][1] << ", " << AnalogAverageMatrix [ROW][2] << ", " << AnalogAverageMatrix [ROW][3] << ", " << AnalogAverageMatrix [ROW][4] << endl ;
    // now perform an average [0] -> [4]
    /*ReadVal = 0;
    for (int x = 0; x < 5; x++) {
      ReadVal += AnalogAverageMatrix[ROW][x];
    } */
    // Yes, a bit more flexible solution would be to loop
    ReadVal = AnalogAverageMatrix [ROW][0] + 
              AnalogAverageMatrix [ROW][1] + 
              AnalogAverageMatrix [ROW][2] + 
              AnalogAverageMatrix [ROW][3] + 
              AnalogAverageMatrix [ROW][4] ;
    ReadVal = ReadVal / Average_Depth;
    if (DIAG) Serial << "Average for " << ROW << ": " << "is " << ReadVal << endl;
    return ReadVal;     // The average is returned to calling function
}

/*  These notes need to be moved to _Notes after debugging and testing
    The test matrix is 45 integer values, arranged in 9 rows of 5 columns.
    [ROW] represents the function + specific analog input pin #
    Example:
    A0 is allocated to water temperature.  WaterTemp_AnalogIn is the pin-name variable representing A0
    Functions simply "set" ROW variable, call the analogAverage function with the analog pin name.
    ROW must be specified because the pins are not contiguous... A0, A1, A2, A3, A4, A10, A12, A13, A15 == 9 pins alias names == [0 - 8]
    The averaging matrix is hard-coded at the moment (easily expanded) to maintain 4 old values and the current analog input.
    The loop() cycle time is still under 1000mS, so the OLED displays should come to read average values in under 5 seconds.

Modified 20190712
  Base code provided by Bill as edited (arrays) version of Europa9_Mega2560 ----> Europa10_Mega2560
  
  Mofified all analogRead statements to average across Average_Depth as AReadAvg[Y][Average_Depth] where
    Y == [0] = WaterTemp_AnalogIn     X = [0], [1], [2], [3], [4]
         [1] = OilTemp_AnalogIn       X = [0], [1], [2], [3], [4]
         [2] = OilPres_AnallgIn       X = [0], [1], [2], [3], [4]
         [3] = FrontCylTemp_AnalogIn  X = [0], [1], [2], [3], [4]
         [4] = RearCylTemp_AnalogIn   X = [0], [1], [2], [3], [4]
         [5] = Ampres_AnalogIn        X = [0], [1], [2], [3], [4]
         [6] = Voltage_AnalogIn       X = [0], [1], [2], [3], [4]
         [7] = FuelQty_AnalogIn       X = [0], [1], [2], [3], [4]
         [8] = FuelPres_AnalogIn      X = [0], [1], [2], [3], [4]
 */


Hi thx for the response @mrburnette i will try to test this method out tomorrow. However i am a bit worried over the string values:

void loop() {
  String values;
  for (int pin = 0; pin < pin_count;pin++){                                    
    float V = analogRead(inputpins[pin])*(Vcc/1023.0);    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    values += "|";
    values += V;
    values += "| ";                                     // append the readings to the string of values followed by a "|"
  }

Because it does not seem like i am able to get that into the brute-forcing method, but it might just be me looking at this wrong?

http://arduiniana.org/libraries/streaming/

after eliminating the oldest reading

shouldn't the code add the latest reading,

not some sum of readings

are both pin and chan indices valid -- total [pin] and total [chan]

you might also consider leaky integration
A[n] += (samp [n] - A [n-1]) * K // K < 1

    samp = analogRead(inputpins[pin])*(Vcc/1023.0)
    total [chan] += (samp - total[chan]) / 8;

Hi i am sorry for the late reply @gcjr . I have tried to use your code to see if it would work. so i changed it like this:

const int numReadings = 5;   // value to determine the size of the readings array.
const int numChannels = 16;
int readings[numChannels][numReadings];      // the readings from the analog input
int index;              // the index of the current reading
int total [numChannels];               // the running total
int average [numChannels];             // the average
int samp;

  for (int chan = 0; chan < numChannels; ++chan ) {
    total[chan] = total[chan] - readings[chan][index]; 
  for (int pin = 0; pin < pin_count;pin++){                                    
    samp = analogRead(inputpins[pin])*(Vcc/1023.0);
    total[chan] += (samp - total[chan]) / 8;
  }
    index = index + 1; // advance to the next position in the array
    if (index >= numReadings){ // if we’re at the end of the array
      index = 0; //wrap around to the beginning
    }
    average[chan] = total[chan] / 5; //Averages the readings
}

The serial monitor just keeps giving me 0 on the sum:

| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|3.28| |2.68| |2.27| |2.02| |1.80| |1.64| |1.55| |1.47| |1.52| |1.41| |1.36| |1.29| |1.27| |1.20| |1.19| |1.29|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 |
| A0 | | A1 | | A2 | | A3 | | A4 | | A5 | | A6 | | A7 | | A8 | | A9 | | A10 | | A11 | | A12 | | A13 | | A14 | | A15 |
|3.28| |2.87| |2.55| |2.37| |2.22| |2.14| |2.10| |2.11| |2.07| |1.97| |1.92| |1.88| |1.85| |1.79| |1.75| |1.79|
Sum:A0 Sum:A1 Sum:A2 Sum:A3 Sum:A4 Sum:A5 Sum:A6 Sum:A7 Sum:A8 Sum:A9 Sum:A10 Sum:A11 Sum:A12 Sum:A13 Sum:A14 Sum:A15
| 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 | | 0 |

so i assume i am doing something wrong ?

The inspiration that i had was from [Smoothing multiple sensors] on how to do smoothing on multiple sensors.

const int numReadings = 5;   // value to determine the size of the readings array.
const int numChannels = 16;
int readings[numChannels][numReadings];      // the readings from the analog input
int index;              // the index of the current reading
int total [numChannels];               // the running total
int average [numChannels];             // the average

  for (int chan = 0; chan < numChannels; ++chan ) {
    total[chan] = total[chan] - readings[chan][index]; 
  for (int pin = 0; pin < pin_count;pin++){                                    
    total[pin] += analogRead(inputpins[pin])*(Vcc/1023.0);    // read voltage from pin (have to convert the string entry into 'int' first by removing the 0)
    total[chan] = total[pin];
  }
  //   total[chan] +=analogRead(inputpins[chan])*(Vcc/1023.0);
  //  total[chan] = total[chan] + readings[chan][index];
    index = index + 1; // advance to the next position in the array
    if (index >= numReadings){ // if we’re at the end of the array
      index = 0; //wrap around to the beginning
    }
    average[chan] = total[chan] / 5; //Averages the readings
}

therefore i tried to do something similar but i am having difficulties on how to do this correctly. Is there a better way ?

it doesn't look like you understood what i suggested

i'm missing something

what is the relationship between pin and chan? don't understand why you removed the last sample using chan as the indicate and then update the total using pin as the index

why are pin and chan being used as indices into total[]?

@gcjr you are very right i dont know why i decided to have both chan and pin so i decided to take the code into a seperate file and work on it and got it working :D.
The new code is this:

const int inputpins[16] = { A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15 };  // define the pins being measured
int pin_count = sizeof(inputpins) / sizeof(inputpins[0]);                                            // Count the number of pins

const int numReadings = 5.0;  // value to determine the size of the readings array.
const int numChannels = 16.0;
int readings[numChannels][numReadings];  // the readings from the analog input
int index;                               // the index of the current reading
int total[numChannels];                  // the running total
int average[numChannels];                // the average

float Vcc = 5.01;  // Variable to match the analogReference, this is added mostly for design
float T;
float A;
float R;

void setup() {
  analogReference(DEFAULT);
  //  analogReference(INTERNAL2V56);                                       // Use interal 2.56v ref

  Serial.begin(9600);
}

void loop() {
  String A;
  for (int pin = 0; pin < pin_count; pin++) {
    total[pin] = total[pin] - readings[pin][index];
    readings[pin][index] = analogRead(inputpins[pin]);
    total[pin] += readings[pin][index];
    index = index + 1;
    if (index >= numReadings) {  // if we’re at the end of the array
      index = 0;                 //wrap around to the beginning
    }
    T = total[pin] * (Vcc / 1023.0);
    R = numReadings;
    A += "|";
    A += T / R;
    A += "| ";  // append the readings to the string of values followed by a "|"
  }

  Serial.print(A);  // send values to serial
  Serial.println();
  delay(500);  // delay in between reads for stability
  Serial.println();
  delay(1);  // delay in between reads for stability
  Serial.println(index);
  delay(1);  // delay in between reads for stability
}

Dont know if i should even show this code but maybe someone will have a similar issue at another date? or just close this ?