Newer
Older
Potentiometer::Potentiometer(int pin, int id) : _pin(pin), _id(id)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
pinMode(pin, INPUT);
EMA_S = analogRead(_pin); //set EMA S for t=1
_value = EMA_S;
_previousReading = EMA_S;
_previousTimer = millis();
_inputFlag = true;
};
void Potentiometer::check()
{
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
int reading = _read();
int deltaValue = abs(reading - _value);
if (reading != _value)
{
_inputFlag = true;
}
if (_inputFlag == true && deltaValue > 1)
{
_inputFlag = false;
_changeFlag = true;
_previousTimer = timer;
_setValue(reading);
_cb(this, kEventUnstableUpdate, getValue());
}
if (_changeFlag == true && deltaTime > _interval)
{
_changeFlag = false;
_cb(this, kEventStableUpdate, getValue());
}
};
void Potentiometer::_setValue(int x)
{
_value = x;
};
int Potentiometer::getValue()
{
return _value;
};
int Potentiometer::_read()
{
int sensorValue = analogRead(_pin); //read the sensor value using ADC
EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)*EMA_S); //run the EMA
int mappedValue = map(EMA_S, 5, 1023, 0, 100);
return mappedValue;
};
int Potentiometer::getId()
{
return _id;
};
void Potentiometer::setEventHandler(void (*function)(Potentiometer*, uint8_t, uint8_t))
{
_cb = function;