|
0
|
1 #include "Arduino.h"
|
|
|
2 #include "Potentiometer.h"
|
|
|
3
|
|
2
|
4
|
|
0
|
5 /**
|
|
|
6 see Potentiometer.h for description.
|
|
|
7 */
|
|
|
8
|
|
|
9 Potentiometer::Potentiometer() {}
|
|
|
10
|
|
|
11 void Potentiometer::setup(byte pin) {
|
|
|
12 this->pin = pin;
|
|
|
13 pinMode(this->pin, INPUT);
|
|
5
|
14 this->isStopped = false;
|
|
|
15 this->sum = 0.0;
|
|
|
16 this->avg = 0.0;
|
|
|
17 this->num = 0;
|
|
0
|
18 }
|
|
|
19
|
|
|
20 void Potentiometer::update() {
|
|
|
21 int v = analogRead(this->pin);
|
|
|
22 // if value is within jitter threshold of current value, add to average
|
|
|
23 if(abs(this->avg - v) <= Potentiometer::JITTER_THRESHOLD) {
|
|
|
24 if(this->num == Potentiometer::MAX_SAMPLES) {
|
|
|
25 this->sum -= this->avg;
|
|
|
26 this->num--;
|
|
1
|
27 this->isStopped = true;
|
|
0
|
28 }
|
|
|
29 this->sum += v;
|
|
|
30 this->num++;
|
|
|
31 this->avg = this->sum/this->num;
|
|
|
32 this->value = (int)(this->avg+.5);
|
|
|
33 }
|
|
|
34 // otherwise reset average
|
|
|
35 else {
|
|
|
36 this->value = v;
|
|
|
37 this->sum = v;
|
|
|
38 this->avg = v;
|
|
|
39 this->num = 1;
|
|
1
|
40 this->isStopped = false;
|
|
0
|
41 }
|
|
|
42 #ifdef DEBUG
|
|
|
43 Serial.print("pin=");
|
|
|
44 Serial.print(pin);
|
|
|
45 Serial.print(", sum=");
|
|
|
46 Serial.print(sum);
|
|
|
47 Serial.print(", num=");
|
|
|
48 Serial.print(num);
|
|
|
49 Serial.print(", avg=");
|
|
2
|
50 Serial.print(avg);
|
|
|
51 Serial.print(", v=");
|
|
|
52 Serial.print(value);
|
|
1
|
53 Serial.print(", isStopped=");
|
|
|
54 Serial.println(this->isStopped);
|
|
0
|
55 #endif
|
|
|
56 }
|
|
|
57
|
|
|
58
|
|
|
59 void Potentiometer::update(unsigned long ms) {
|
|
|
60 this->update();
|
|
|
61 }
|
|
|
62
|
|
|
63
|
|
|
64
|