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);
instead of (without normalizing):
x = analogRead(xaxis);
y = analogRead(yaxis);
z = analogRead(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?
[edit]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.
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.
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.