How to do the "smoothing" with two different sensors?

Hi buddies,
My name is Angelo and I’m working with 2 pressure sensor where them output range is 1 to 5V.
I need to use the function “smoothing” both of sensors but I can’t.
I can work with just one. Can anyone help me???
This is my program:

// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
#include <LiquidCrystal.h>

const int numReadingsP = 10;

int readingsP[numReadingsP];      // the readings from the analog input
int indexP = 0;                  // the index of the current reading
int totalP = 0;                  // the running total
int averageP = 0;                // the average

int inputPinP = A0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
  lcd.begin(16, 2);
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadingsP; thisReading++)
    readingsP[thisReading] = 0; 


void loop() {
  // subtract the last reading:
  totalP= totalP - readingsP[indexP];  
  // read from the sensor:  
  readingsP[indexP] = analogRead(inputPinP); 
  // add the reading to the total:
  totalP= totalP + readingsP[indexP];  
  // advance to the next position in the array:  
  indexP = indexP + 1;                    

  // if we're at the end of the array...
  if (indexP >= numReadingsP)          
    // ...wrap around to the beginning: 
    indexP = 0;                           

  // calculate the average:
  averageP = totalP / numReadingsP;   
  float voltagemP = averageP * (5.0 / 1023.0);
  float milimetrosP = (voltagemP * 63.69) - 63.7;
  lcd.setCursor(0, 0);
  lcd.print("D.P: ");
  lcd.print("mm/H2O           ");

  delay(300);        // delay in between reads for stability

Best Regards

Ângelo Ventura
from Brazil.

Moderator edit: code tag corrected

Are you asking a programming question or a sensor question?

If this is a programming question then the general solution employs another array index. “General” means that is works for any number of sensors limited only by memory.

Thus totalP becomes totalP, averageP becomes averageP_, inputPinP becomes inputP*,_
_readingsP[numReadingsP] becomes readingsP[numReadingsP], etc.
The new index runs from 0 to 1 for 2 sensors, 0-9 for ten sensors.
Put most of your loop code in a seperate function such as smoothData(int i)
In the loop you call it as

for (int i = 0; i < numSensors;i++)

Another possibility, much simpler to use, is a low pass filter. For example:

reading = analogRead(inputPinP); 
filteredReading = ( filteredReading*7 + reading)/8;

filteredReading must be initialized in setup() to zero or some reasonable value and will approach the smoothed average after a few measurements. Mind that filteredReading is unsigned int or long, so that it doesn't overflow during the multiplication. To make this filter respond more quickly or slowly, use different scale factors such as.

filteredReading = (filteredReading*3 + reading)/4;


filteredReading = (filteredReading*15 + reading)/16;

I like to use powers of two because the division can be replaced by a shift, for example (x/16) is equal to (x>>4) but is faster to calculate.

filteredReading must be initialized in setup() to zero or some reasonable value

Setting it to zero as initial value is often a bad choice (unless the expected value is 0). as that will affect the first dozen measurements.

What I often do is to make a first read in setup(), then it approaches the smoothed value instantly.

  filteredReading = analogRead(inputPinP)

  filteredReading = ( filteredReading*7 + reading)/8;

alternative you can use libraries that help, have a look at:

You'll get bad rounding errors with that code, integer division is truncating and thus biased (you need to understand about quantization issues and limit cycles to use integer or fixed point maths in a digital filter.

Use floats for digital filters if you can afford to, generally you will avoid such artifacts:

  filteredReading = ( filteredReading*7 + reading)/8;

Can be improved:

  filteredReading = ( filteredReading*7 + reading + 4) >> 3 ;  // unbiased rounding

Scaling the input value by 8 or more would certainly help prevent losing lots of precision. Note that arithmetic shift right after adding the de-bias value behaves better than integer division if values can be negative (division truncates towards zero)

Or in floating point and rewritten to use only one coefficient:

float filteredReading, reading ;

   filteredReading += 0.125 * (reading - filteredReading) ;

This form of the recurrence needs just one coefficient and is guaranteed unity gain at DC. You can also think of it as a PID controller with just a P term.