Skip to content

Commit

Permalink
Add an optional parameter to the constructor to cause getAvg()
Browse files Browse the repository at this point in the history
to return zero instead of causing a divide-by-zero situation
if it is called before any readings are recorded.
  • Loading branch information
JChristensen committed Nov 4, 2024
1 parent 23290e2 commit 63dfeb5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/gpl.html>
The user specifies the interval (number of data points) for the moving average in the constructor. When the `begin()` function is called, an array is dynamically allocated to hold the number of data points in the interval. This array is never deallocated, and the user should call `begin()` only once (for a given `movingAvg` instance) in setup or other initialization code. Dynamic allocation is used strictly with the intent of creating the proper size array for the user's purposes, and not to free up the memory at a later point. It is strongly recommended that `movingAvg` objects remain allocated as long as the code is running. Failure to observe these guidelines can result in heap fragmentation, crashes and other undesired behavior.

## Constructor
### movingAvg(int interval)
### movingAvg(int interval, bool avoidDivByZero=false)
##### Description
Defines a `movingAvg` object where the average is calculated using *interval* data points.
##### Syntax
`movingAvg(interval);`
##### Parameters
**interval:** The number of data points to use when calculating the moving average. *(int)*
**interval:** The number of data points to use when calculating the moving average. *(int)*

**avoidDivByZero:** Optional parameter that causes `getAvg()` ([see caution below](#getavg)) to return zero if called before any readings are recorded. *(bool, defaults to false if not given)*
##### Returns
None.
##### Example
Expand All @@ -53,6 +55,7 @@ mySensor.begin();
### reading(int dataPoint)
##### Description
Adds a new data point to the moving average. Returns the new moving average value. Until the interval array is filled, the average is calculated from those data points already added, i.e. a fewer number of points than defined by the constructor - thanks to Tom H. (Duckie) for this idea!

##### Syntax
`reading(dataPoint);`
##### Parameters
Expand All @@ -68,6 +71,8 @@ int sensorMovingAvg = mySensor.reading(sensorData);
### getAvg()
##### Description
Returns the current moving average value without adding a new reading.

**Caution:** If `getAvg()` is called before any data points (readings) are recorded, a divide-by-zero situation will occur, likely with unpredictable/undesirable results. To instead have `getAvg()` return a value of zero in this case, set the optional `avoidDivByZero` parameter in the [constructor](#movingavgint-interval-bool-avoiddivbyzerofalse) to `true`. Note that this may require the caller to implement additional logic to differentiate between a "no data" situation and one where the average is actually zero.
##### Syntax
`getAvg();`
##### Parameters
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=movingAvg
version=2.3.1
version=2.3.2
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=A simple Arduino library for calculating moving averages.
Expand Down
5 changes: 4 additions & 1 deletion src/movingAvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ int movingAvg::reading(int newReading)
// just return the current moving average
int movingAvg::getAvg()
{
return (m_sum + m_nbrReadings / 2) / m_nbrReadings;
if (m_nbrReadings || !m_avoidDivByZero)
return (m_sum + m_nbrReadings / 2) / m_nbrReadings;
else
return 0;
}

// return the average for a subset of the data, the most recent nPoints readings.
Expand Down
16 changes: 9 additions & 7 deletions src/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
class movingAvg
{
public:
movingAvg(int interval)
: m_interval{interval}, m_nbrReadings{0}, m_sum{0}, m_next{0} {}
movingAvg(int interval, bool avoidDivByZero=false)
: m_interval{interval}, m_avoidDivByZero{avoidDivByZero},
m_nbrReadings{0}, m_sum{0}, m_next{0} {}
void begin();
int reading(int newReading);
int getAvg();
Expand All @@ -20,10 +21,11 @@ class movingAvg
int* getReadings() {return m_readings;}

private:
int m_interval; // number of data points for the moving average
int m_nbrReadings; // number of readings
long m_sum; // sum of the m_readings array
int m_next; // index to the next reading
int* m_readings; // pointer to the dynamically allocated interval array
int m_interval; // number of data points for the moving average
bool m_avoidDivByZero; // return a zero average if requested when there are no data points
int m_nbrReadings; // number of readings
long m_sum; // sum of the m_readings array
int m_next; // index to the next reading
int* m_readings; // pointer to the dynamically allocated interval array
};
#endif

0 comments on commit 63dfeb5

Please sign in to comment.