10 #define SMOOTHED_AVERAGE 1 11 #define SMOOTHED_EXPONENTIAL 2 20 byte smoothReadingsFactor = 10;
21 byte smoothReadingsPosition = 0;
22 byte smoothReadingsNum = 0;
27 bool begin (byte smoothMode, byte smoothFactor = 10);
28 bool add (T newReading);
43 delete[] smoothReading;
50 smoothReadingsFactor = smoothFactor;
53 case SMOOTHED_AVERAGE :
55 smoothReading =
new T[smoothReadingsFactor];
58 for (
int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
59 smoothReading[thisReading] = 0;
65 case SMOOTHED_EXPONENTIAL :
67 smoothReading =
new T[2];
85 case SMOOTHED_AVERAGE :
87 if(smoothReadingsNum < smoothReadingsFactor) { smoothReadingsNum++; }
89 smoothReading[smoothReadingsPosition] = newReading;
91 if (smoothReadingsPosition == (smoothReadingsFactor - 1)) {
92 smoothReadingsPosition = 0;
94 smoothReadingsPosition++;
100 case SMOOTHED_EXPONENTIAL :
102 if( smoothReadingsNum == 0 ) {
104 smoothReading[0] = newReading;
106 smoothReading[0] = (T)(((
long double)smoothReadingsFactor/100) * newReading + (1 - ((
long double)smoothReadingsFactor/100)) * smoothReading[0]);
109 smoothReading[1] = newReading;
121 template <
typename T>
123 switch (smoothMode) {
124 case SMOOTHED_AVERAGE : {
127 for (
int x = 0; x < smoothReadingsNum; x++) {
128 runningTotal += smoothReading[x];
131 return runningTotal / smoothReadingsNum;
135 case SMOOTHED_EXPONENTIAL :
136 return smoothReading[0];
146 template <
typename T>
148 switch (smoothMode) {
149 case SMOOTHED_AVERAGE :
151 if (smoothReadingsPosition == 0) {
152 return smoothReading[smoothReadingsFactor-1];
154 return smoothReading[smoothReadingsPosition-1];
158 case SMOOTHED_EXPONENTIAL :
159 return smoothReading[1];
169 template <
typename T>
171 switch (smoothMode) {
172 case SMOOTHED_AVERAGE :
174 smoothReadingsPosition = 0;
175 smoothReadingsNum = 0;
178 for (
int thisReading = 0; thisReading < smoothReadingsNum; thisReading++) {
179 smoothReading[thisReading] = 0;
183 case SMOOTHED_EXPONENTIAL :
184 smoothReadingsNum = 0;
185 smoothReading[0] = 0;
186 smoothReading[1] = 0;