2020-11-27 05:10:47 -05:00
|
|
|
#pragma once
|
2013-09-29 08:33:29 -04:00
|
|
|
//
|
|
|
|
// FILE: AnalogPin.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-28 15:33:10 -04:00
|
|
|
// VERSION: 0.2.7
|
2020-11-27 05:10:47 -05:00
|
|
|
// DATE: 2013-09-09
|
|
|
|
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
|
|
|
|
// URL: https://github.com/RobTillaart/AnalogPin
|
2013-09-29 08:33:29 -04:00
|
|
|
//
|
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
|
2014-12-07 15:11:55 -05:00
|
|
|
#include "Arduino.h"
|
2013-09-29 08:33:29 -04:00
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
#define ANALOGPIN_LIB_VERSION (F("0.2.7"))
|
2021-10-17 13:20:48 -04:00
|
|
|
|
2013-09-29 08:33:29 -04:00
|
|
|
|
|
|
|
class AnalogPin
|
|
|
|
{
|
2014-12-07 15:11:55 -05:00
|
|
|
public:
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
explicit AnalogPin(const uint8_t pin);
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
// prescale = { 2..7 }, 2 is bad, 3 is pretty noisy, 4 and 5 are acceptable, 6 and 7 are good. Depends on project!!!
|
|
|
|
// time indication per analogRead for different prescale values on UNO
|
|
|
|
// 2 => 14 uSec 5 => 38 uSec
|
|
|
|
// 3 => 18 uSec 6 => 63 uSec
|
|
|
|
// 4 => 24 uSec 7 => 120 uSec (default/normal)
|
2021-10-17 13:20:48 -04:00
|
|
|
void setPrescaler(const uint8_t prescale = 7);
|
2021-01-29 06:31:58 -05:00
|
|
|
inline uint8_t getPrescaler(void) const { return _prescale; };
|
2014-12-07 15:11:55 -05:00
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
// noise 0..255; in practice only small values are used (0..10).
|
2021-01-29 06:31:58 -05:00
|
|
|
inline void setNoiseThreshold(const uint8_t noise = 0) { _noise = noise; };
|
|
|
|
inline uint8_t getNoiseThreshold(void) const { return _noise; };
|
2022-10-28 15:33:10 -04:00
|
|
|
|
|
|
|
// alpha = 0..31;
|
2021-10-17 13:20:48 -04:00
|
|
|
void setSmoothWeight(const uint8_t alpha = 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
inline uint8_t getSmoothWeight(void) const { return _alpha; };
|
2014-12-07 15:11:55 -05:00
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
// twice=true to do analogRead twice to reduce noise
|
2021-01-29 06:31:58 -05:00
|
|
|
int read(const bool twice = false);
|
|
|
|
int readSmoothed();
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
// expose internal data as that might be useful.
|
|
|
|
inline int readPrevious(void) const { return _prevValue; }
|
2021-01-29 06:31:58 -05:00
|
|
|
inline int readLast(void) const { return _value; }
|
2014-12-07 15:11:55 -05:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
private:
|
|
|
|
void _rawRead();
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t _pin;
|
|
|
|
uint8_t _alpha;
|
|
|
|
uint8_t _noise;
|
|
|
|
uint8_t _prescale;
|
|
|
|
int _value;
|
|
|
|
int _prevValue;
|
2013-09-29 08:33:29 -04:00
|
|
|
};
|
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-12 09:57:24 -05:00
|
|
|
|