Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
EMASonicRanger.cpp
1 #include <Arduino.h>
2 #include <Millis.h>
3 #include "EMASonicRanger.h"
4 
5 EMASonicRanger::EMASonicRanger(const uint8_t trigPin, const uint8_t echoPin, const uint16_t periodMs, const float alpha, uint16_t minimumRange) :
6  SonicRanger(trigPin, echoPin),
7  _periodMs(periodMs),
8  _alpha(alpha),
9  _minimumRange(minimumRange)
10 {
11 }
12 
14 {
16  _lastUpdated = Millis();
17  _movingAverage = SonicRanger::getRange();
18  _lastSample = _movingAverage;
19 }
20 
22 {
23  if (_periodMs == 0 || MillisSince(_lastUpdated) >= _periodMs) {
24  uint16_t sample = SonicRanger::getRange();
25  if (sample >= _minimumRange) {
26  _lastSample = sample;
27  _movingAverage = (_alpha*_lastSample) + ((1-_alpha)*_movingAverage);
28  _lastUpdated = Millis();
29  }
30  }
31 }
32 
34 {
35  return _movingAverage;
36 }
37 
38 
uint16_t getRange()
Definition: SonicRanger.cpp:55
EMASonicRanger(const uint8_t trigPin, const uint8_t echoPin, const uint16_t periodMs=10, const float alpha=0.5, const uint16_t minimumRange=3)
void begin()
Definition: SonicRanger.cpp:49
uint16_t getRange()