forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalogPin.cpp
More file actions
37 lines (33 loc) · 725 Bytes
/
Copy pathAnalogPin.cpp
File metadata and controls
37 lines (33 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// FILE: AnalogPin.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: wrapper class for analogRead
// URL:
//
// Released to the public domain
//
#include "AnalogPin.h"
AnalogPin::AnalogPin(uint8_t pin)
{
_pin = pin;
_prevValue = analogRead(pin);
}
uint16_t AnalogPin::read(uint8_t noise)
{
uint16_t value = analogRead(_pin);
if (noise == 0 || abs(value - _prevValue) > noise)
{
_prevValue = value;
}
return _prevValue;
}
uint16_t AnalogPin::readSmoothed(uint8_t alpha)
{
alpha = constrain(alpha, 0, 32);
uint16_t value = analogRead(_pin);
value = (alpha*_prevValue + (32-alpha)*value)/32;
_prevValue = value;
return value;
}
// -- END OF FILE --