GY-63_MS5611/libraries/RunningAverage/examples/ra_FastAverageTest/ra_FastAverageTest.ino

95 lines
1.7 KiB
Arduino
Raw Normal View History

//
// FILE: ra_FastAverageTest.ino
// AUTHOR: Rob Tillaart
// DATE: 2015-sep-04
// PUPROSE: demo to see if different average algorithm give different result
2021-11-24 04:03:41 -05:00
#include "RunningAverage.h"
2020-11-27 05:33:55 -05:00
RunningAverage myRA(16);
2020-11-27 05:33:55 -05:00
float avg = 0;
float favg = 0;
float diff = 0;
float maxDiff = 0;
2020-11-27 05:33:55 -05:00
uint32_t start, stop;
2021-11-24 04:03:41 -05:00
void setup(void)
{
Serial.begin(115200);
Serial.print("\nDemo ");
Serial.println(__FILE__);
Serial.print("Version: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
myRA.clear(); // explicitly start clean
2020-11-27 05:33:55 -05:00
measure_duration();
}
2021-11-24 04:03:41 -05:00
2020-11-27 05:33:55 -05:00
void loop()
{
2020-11-27 05:33:55 -05:00
test(1000000);
}
void measure_duration()
{
myRA.fillValue(100, 16);
start = micros();
favg = myRA.getFastAverage();
stop = micros();
Serial.print("getFastAverage: ");
Serial.println(stop - start);
delay(10);
2020-11-27 05:33:55 -05:00
myRA.fillValue(100, 16);
start = micros();
favg = myRA.getAverage();
stop = micros();
Serial.print(" getAverage: ");
Serial.println(stop - start);
delay(10);
Serial.println();
}
void test(long n)
{
Serial.println("\nCNT\tAVG\t\tFASTAVG\t\tDIFF\t\tMAXDIFF");
for (long i = 0; i < n; i++)
{
2020-11-27 05:33:55 -05:00
long rn = random(0, 1000);
myRA.addValue(rn * 0.001);
2021-11-24 04:03:41 -05:00
if ( i % 1000 == 0)
{
2020-11-27 05:33:55 -05:00
// the order of the next two lines is important as getAverage() resets the _sum
2021-11-24 04:03:41 -05:00
// used by the getFastAverage();
2020-11-27 05:33:55 -05:00
favg = myRA.getFastAverage();
avg = myRA.getAverage();
diff = abs(avg - favg);
if (diff > maxDiff) maxDiff = diff;
Serial.print(i);
Serial.print("\t");
Serial.print(avg, 7);
Serial.print("\t");
Serial.print(favg, 7);
Serial.print("\t");
Serial.print(diff, 7);
Serial.print("\t");
Serial.print(maxDiff, 7);
Serial.println();
}
}
2020-11-27 05:33:55 -05:00
delay(100);
}
2020-11-27 05:33:55 -05:00
2021-11-24 04:03:41 -05:00
2020-11-27 05:33:55 -05:00
// -- END OF FILE --
2021-11-24 04:03:41 -05:00