2013-09-29 14:33:29 +02:00
|
|
|
//
|
|
|
|
// FILE: AnalogPin.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2014-12-07 21:11:55 +01:00
|
|
|
// VERSION: 0.1.02
|
|
|
|
// DATE: 2014-10-05
|
2013-09-29 14:33:29 +02:00
|
|
|
// PURPOSE: wrapper class for analogRead
|
2014-12-07 21:11:55 +01:00
|
|
|
//
|
|
|
|
// HISTORY:
|
|
|
|
// 0.1.00 - 2013-09-09 initial version
|
|
|
|
// 0.1.01 - 2013-11-09 added some comments
|
|
|
|
// 0.1.02 - 2014-10-05 changed signatures datatypes
|
2013-09-29 14:33:29 +02:00
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AnalogPin.h"
|
|
|
|
|
|
|
|
AnalogPin::AnalogPin(uint8_t pin)
|
|
|
|
{
|
|
|
|
_pin = pin;
|
|
|
|
_prevValue = analogRead(pin);
|
|
|
|
}
|
|
|
|
|
2014-12-07 21:11:55 +01:00
|
|
|
int AnalogPin::read(uint8_t noise)
|
2013-09-29 14:33:29 +02:00
|
|
|
{
|
2014-12-07 21:11:55 +01:00
|
|
|
int value = analogRead(_pin);
|
2013-09-29 14:33:29 +02:00
|
|
|
if (noise == 0 || abs(value - _prevValue) > noise)
|
|
|
|
{
|
|
|
|
_prevValue = value;
|
|
|
|
}
|
|
|
|
return _prevValue;
|
|
|
|
}
|
|
|
|
|
2014-12-07 21:11:55 +01:00
|
|
|
int AnalogPin::readSmoothed(uint8_t alpha)
|
2013-09-29 14:33:29 +02:00
|
|
|
{
|
2014-12-07 21:11:55 +01:00
|
|
|
alpha = constrain(alpha, 0, 31);
|
|
|
|
int value = analogRead(_pin);
|
2013-09-29 14:33:29 +02:00
|
|
|
value = (alpha*_prevValue + (32-alpha)*value)/32;
|
|
|
|
_prevValue = value;
|
|
|
|
return value;
|
|
|
|
}
|
2014-12-07 21:11:55 +01:00
|
|
|
|
|
|
|
int AnalogPin::readPrevious()
|
|
|
|
{
|
|
|
|
return _prevValue;
|
|
|
|
}
|
2013-09-29 14:33:29 +02:00
|
|
|
// -- END OF FILE --
|