Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
EMASampler.cpp
1 #include <Arduino.h>
2 #include "MutilaDebug.h"
3 #include "EMASampler.h"
4 #include "Millis.h"
5 
6 EMASampler::EMASampler(const uint8_t pin, const uint16_t periodMs, const float alpha) :
7  AbstractSampler(pin, periodMs),
8  _alpha(alpha),
9  _lastUpdated(0),
10  _movingAverage(0.),
11  _lastSample(0)
12 {
13 }
14 
16 {
17  _DBLN(F("EMASampler::begin"));
18  // Note: no need to set pinMode for analog inputs
19  _movingAverage = analogRead(_pin);
21  _lastUpdated = Millis();
22 }
23 
25 {
26  if (_periodMs == 0 || MillisSince(_lastUpdated) >= _periodMs) {
27  _lastSample = analogRead(_pin);
28  _DB(F("EMASampler::update sample="));
29  _DBLN(_lastSample);
31  _lastUpdated = Millis();
32  }
33 }
34 
35 
36 
float _alpha
alpha value used in EMA calculation
Definition: EMASampler.h:70
const uint8_t _pin
pin to read data from
const uint16_t _periodMs
minimum ms
float _movingAverage
most recently calculated mean value
Definition: EMASampler.h:72
uint32_t _lastUpdated
when last sample taken
Definition: EMASampler.h:71
EMASampler(const uint8_t pin, const uint16_t periodMs=10, const float alpha=0.5)
Definition: EMASampler.cpp:6
int16_t _lastSample
keep the most recent sample
Definition: EMASampler.h:73
virtual void begin()
Definition: EMASampler.cpp:15
virtual void update()
Definition: EMASampler.cpp:24
Base class for analog pin sampling classes.