Skip to content

Commit

Permalink
Add reset() function. Closes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Mar 31, 2018
1 parent 446a705 commit 52a0266
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ movingAvg KEYWORD1
begin KEYWORD2
reading KEYWORD2
getAvg KEYWORD2
reset KEYWORD2
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.0.0
version=2.1.0
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=A simple Arduino library for calculating moving averages.
Expand Down
10 changes: 9 additions & 1 deletion src/movingAvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
3 changes: 2 additions & 1 deletion src/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 52a0266

Please sign in to comment.