I was working on a project that is reading an analog input on my Uno. The input seams a bit noisy as the value jumps around a little without a change to the actual input ( I was using a linear POT connected to 5v and gnd with the center wiper going to the input).
So I was looking for examples of smoothing code and ran onto the tutorial here on the site. I implemented the code in my design and it works well although the averaged response to the change can take a while to influence the result (I already knew that though). I spent some time looking at the code and trying to come up with ways to improve it without changing the size of the array.
I doing that I realized that there was an error int eh code as it was originally posted. The error is in the main loop in the following section:
readings[index] = analogRead(inputPin); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1;
// if we're at the end of the array... if (index >= numReadings) The check is for greater than or equal to but the current index has not been used yet. Thus readings[numReadings] is never actually changed from the initialization value. // ...wrap around to the beginning: index = 0;
// calculate the average: average = total / numReadings;
As noted above (in red) there is a test to see if the index for the Array is greater than or equal to the value set for the last index in the array. However, the index was incremented one line above that and the current index location in the array has not been used. Then the index is set to 0. The issue with this is that the rest of the logic assumes that there are numReadings values in the sum that is averaged in the calculation. Thus total will contain the sum of n values and it is divided by n+1.
So even on the best of days chances are your averaged value will never reach the true average even if the input could be held at one value for numReadings times through the loop. It's a small issue and can easily be fixed by changing the line to read:
if (index > numReadings)
instead of the original code. The result is probably close enough for most times that this is used but for what I was doing it was causing me problems. Hope this helps someone else out that was having the similar issue that I was. Krazie