diff --git a/README.md b/README.md index 128c5a7..80befdc 100644 --- a/README.md +++ b/README.md @@ -78,3 +78,17 @@ The moving average value. *(int)* ```c++ int sensorMovingAvg = mySensor.getAvg(); ``` + +### reset() +##### Description +Restarts the moving average. Zeros the interval array and associated data. +##### Syntax +`reset();` +##### Parameters +None. +##### Returns +None. +##### Example +```c++ +mySensor.reset(); +``` diff --git a/keywords.txt b/keywords.txt index 9776644..693c28f 100644 --- a/keywords.txt +++ b/keywords.txt @@ -2,3 +2,4 @@ movingAvg KEYWORD1 begin KEYWORD2 reading KEYWORD2 getAvg KEYWORD2 +reset KEYWORD2 diff --git a/library.properties b/library.properties index b3353ee..d886a49 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=movingAvg -version=2.0.0 +version=2.1.0 author=Jack Christensen maintainer=Jack Christensen sentence=A simple Arduino library for calculating moving averages. diff --git a/src/movingAvg.cpp b/src/movingAvg.cpp index 8f84235..f31768d 100644 --- a/src/movingAvg.cpp +++ b/src/movingAvg.cpp @@ -25,7 +25,7 @@ int movingAvg::reading(int newReading) { m_sum = m_sum - m_readings[m_next] + newReading; } - + m_readings[m_next] = newReading; if (++m_next >= m_interval) m_next = 0; return (m_sum + m_nbrReadings / 2) / m_nbrReadings; @@ -36,3 +36,11 @@ int movingAvg::getAvg() { return (m_sum + m_nbrReadings / 2) / m_nbrReadings; } + +// start the moving average over again +void movingAvg::reset() +{ + m_nbrReadings = 0; + m_sum = 0; + m_next = 0; +} diff --git a/src/movingAvg.h b/src/movingAvg.h index 01d7f07..65781a9 100644 --- a/src/movingAvg.h +++ b/src/movingAvg.h @@ -14,7 +14,8 @@ class movingAvg void begin(); int reading(int newReading); int getAvg(); - + void reset(); + private: int m_interval; // number of data points for the moving average int m_nbrReadings; // number of readings