# multiple input simultaneous smoothing

Hi all, I am fairly new to C++ and I am working with an analog accelerometer with a Arduino Duemilanove. While trying to smooth out the input values by normalizing the data, if I call to three functions like so:

x = smoothX(xaxis); y = smoothY(yaxis); z = smoothZ(zaxis);

and each function takes an average from its respective pin over 10 cycles, will these functions run simultaneously and take only 10 cycles to complete or will it take 30 cycles to obtain all three averages?

They execute consecutively.
(This isn’t `occam`, you know!)

Is there any way to have them executed simultaneously or am I pretty much stuck with what I have?

Is there any way to have them executed simultaneously or am I pretty much stuck with what I have? [/edit]

As you have it the readings will be processed serially, first 10 x readings, 10 y and then ten z readings. However you could write a new smooth_all(x,y,z) function that reads one each of x,y and z analog inputs sequentially in a loop until ten loops are performed and then compute the three averages for x,y and z and then returns to your main loop.

Lefty

Unless you are doing direct port access, either way (get all x, get all y, get all z or get 1 x, 1 y, 1 z 10 times) will take the same amount of time, since the slowest function is the digitalRead.

Thanks for helping me out!

Would the following be a valid loop or do I need to include the averaging within the “while” brackets?

index = 0;

totalX = 0;
totalY = 0;
totalZ = 0;

averageX = 0;
averageY = 0;
averageZ = 0;

{

++index;
}

index = 0;

x = averageX;
y = averageY;
z = averageZ;

That's good.

Keeping a running average, by calculating the average inside the loop would slow things down. Floating point division is slow. Unless totalX, etc. are integers. If they are, calculating the average inside the loop would result in a less accurate value because of more truncation.

So what you're saying is that all of those variables should be floats for accuracy or integers for speed?

That is true.

Thanks so much for everyone's help!