Function to Smooth Multiple Sensor Inputs

Hi all,

I've been attempting to create a MIDI controller using an XY pad (and eventually add more sensors).

At present the signal is a bit noisy and I wish to LPF all of the inputs.

I've created a function to do this based on the smoothing tutorial.

However as this is a simple function (as below) it can only filter one sensor at a time.

[Return] int [RunningAverage] Smoothing(int sensorValue)

The plan is to have:

  • A Structure for the Filter elements (number of readings, the readIndex, runningTotal)
  • A function that has an array of inputs, each one for each sensor.
  • Pointers that pass the values into each filter.

Smoothing(inputNumber, &filterNumber)

Does anyone have a function that does this already or have any advice on how this can be done.

Any resources for further reading will be much appreciated also.

The Arduino can do only one thing at a time: e.g. collect a single data point, or process it, or store it.

If you already have a structure that contains all the information relevant to a particular sensor, then you can pass the address of that structure to a general function that performs a smoothing operation, independent of sensor number.

There is no need to complicate the situation by making a special function that anticipates some number of sensors.

Furthermore, there are many different types of smoothing or low pass filter operations. You can pass the same data structure to different functions and decide which is operation is most suitable.

How about:

byte pins[] = { A0, A1, A2 } ;
float smoothed[3] ;


// one pole IIR digital low pass filter:
void Smoothing(byte inputPin, float &variable)
{
  variable += 0.2 * (analogRead (inputPin) - variable) ;
}

void loop ()
{
  for (byte i = 0 ; i < 3 ; i++)
    Smoothing (pins[i], smoothed[i]) ;
  ....
}

Just needs the variable itself as storage, nothing else required. The 0.2 can be varied to
control the convergence rate.

You could use a class and go for an object-oriented approach:
https://playground.arduino.cc/Main/RunningAverage

Pieter