Analog smoothing of multiple inputs

It gets better with age :confused: No it doesn't.

.

Hi All

So I've been testing my code for a couple of days. And I still can't get to grips with the sampling side of things.

const int NINPUTS = 4;

const byte inputPin[NINPUTS] = { A0, A4, A3, A5 };

int reading[NINPUTS];
float smoothedReading[NINPUTS];

void takeReadings() {
  for(int i = 0; i<NINPUTS; i++) {
    reading[i] = analogRead(inputPin[i]);
    smoothedReading[i] = smoothedReading[i] * .75 + reading[i] * .25;
  }
}

As I understand it.

smoothedReading[i] = smoothedReading[i] * .75 + reading[i] * .25;

Is the sampling side of things.

My problem is that as a non programmer I don't understand it at all. But I wish to learn.

If someone can please explain to me what is happening, I would have a greater understanding of what to adjust, and what effect it has on the outcome.

Regards

Fred

You tell us what 'you' think is going on with each line of code:

const int NINPUTS = 4;
const byte inputPin[NINPUTS] = { A0, A4, A3, A5 };
int reading[NINPUTS];
float smoothedReading[NINPUTS];
void takeReadings() {
for(int i = 0; i<NINPUTS; i++) {
  reading[i ] = analogRead(inputPin[i ]);
  smoothedReading[i ] = smoothedReading[i ] * .75 + reading[i ] * .25;
}
}

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

Declare NINPUTS as a constant integer with a value of 4
Declare input[NINPUTS] with the values of the A0, A4, A3,A5  and the values are a constant byte
Declare reading[NINPUTS] as an integer
Declare smoothedReading[NINPUTS] as a float
Declare the function void takeReadings()
I don't know. Spomething to do with counting the readings I guess
I don't know. Making reading[i] = analogRead(inputPin[i]). But why would you do that
I don't know. Making smoothReading[i] = smoothReading[i] and some confusing equation.

So here's what I don't understand, what is [i]? And how do we know how many readings we are taking per input.

That should be good for a giggle. But I'm not a programmer, and I still struggle with the correct terms.

Regards

Fred

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

It's math. You're taking 25% of the current reading, and adding 75% of the previous reading. If it were 50/50 it would be a standard average. Get it? Then rinse and repeat. The result becomes the previous reading for the next round.