Single Header C++ / Arduino Stream-Average-Library
Disclaimer: This is not a moving average, instead it accumulates the average of the values you add
Go to PlatformIO Home, click on Libraries and search for StreamAverage
Open the library manager by clicking on Tools > Manage Libraries and search for StreamAverage. Then click install.
In this Repository go to Releases. There choose the Version you like an download the library as .zip just the .h file.
First create a StreamAverage object of your desired type by passing as template argument.
StreamAverage<float> average;You can get values into the average by using the add() method
average.add(newValue);or the shift operator
average << newValue;Both methods return the new value of the average, including the newValue
float newAverage;
newAverage = average.add(2);
newAverage = average << 4;
// new Average now contains 3You can get the current average using get()
float currentAverage = average.get();or get it implicitly
float currentAverage = average;You can get the maximum and minimum value the average holds, by calling getMax() and getMin()
average << 5.5;
average << 43.27;
average << -19.3;
average << 0.02;
float maximum = average.getMax(); // 43.27
float minimum = average.getMin(); // -19.3 To find out how many values have been added to the average, use getNumValues()
size_t num = average.getNumValues();After calling the reset() method, get(), getMax(), getMin(), getNumValues() will evalueate to 0
average.reset();