# movingAvg (community library)

## Summary

Name | Value |
---|---|

Name | movingAvg |

Version | 2.1.0 |

Installs | |

Author | Jack Christensen jack.christensen@outlook.com |

Maintainer | Jack Christensen jack.christensen@outlook.com |

URL | https://github.com/JChristensen/movingAvg |

Download | .tar.gz |

A simple Arduino library for calculating moving averages. Useful for smoothing sensor readings, etc. For efficiency, the library operates in the integer domain; therefore the moving average calculation is approximate.

## Example Build Testing

Device OS Version:

This table is generated from an automated build. Success only indicates that the code compiled successfully.

## Library Read Me

*This content is provided by the library maintainer and has not been validated or approved.*

### Arduino Moving Average Library

https://github.com/JChristensen/movingAvg README file Jack Christensen Mar 2012

#### License

Arduino movingAvg Library Copyright (C) 2018 Jack Christensen GNU GPL v3.0

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/gpl.html

#### Description

*movingAvg* is a simple Arduino library for calculating moving averages. It is useful for smoothing sensor readings, etc. For efficiency, the library operates in the integer domain. This means that the calculated moving averages are mathematically approximate. Both data input to the library and the returned moving averages are 16-bit signed integers.

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)

####### 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)*

####### Returns None.

####### Example

```
movingAvg mySensor(10); // use 10 data points for the moving average
```

#### Methods

##### begin()

####### Description
Initializes a `movingAvg`

object. Call `begin()`

once and only once for any given `movingAvg`

instance. See comments in the **Description** section above.

####### Syntax
`begin();`

####### Parameters None.

####### Returns None.

####### Example

```
movingAvg mySensor(10); // use 10 data points for the moving average
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
**dataPoint:** The new data point to be added to the moving average. *(int)*

####### Returns
The new moving average value. *(int)*

####### Example

```
int sensorData = analogRead(SENSOR_PIN);
int sensorMovingAvg = mySensor.reading(sensorData);
```

##### getAvg()

####### Description Returns the current moving average value without adding a new reading.

####### Syntax
`getAvg();`

####### Parameters None.

####### Returns
The moving average value. *(int)*

####### Example

```
int sensorMovingAvg = mySensor.getAvg();
```

##### reset()

####### Description Restarts the moving average. Zeros the interval array and associated data.

####### Syntax
`reset();`

####### Parameters None.

####### Returns None.

####### Example

```
mySensor.reset();
```

## Browse Library Files