annotate Potentiometer.cpp @ 6:4e32bc18a7f7 default tip

adjusted jitter threshold, added ignore heuristic
author ark
date 2017-07-03 00:24 +0200
parents ea44b28e12ab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
1 #include "Arduino.h"
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
2 #include "Potentiometer.h"
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
3
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
4 /**
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
5 see Potentiometer.h for description.
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
6 */
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
7
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
8 Potentiometer::Potentiometer() {}
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
9
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
10 void Potentiometer::setup(byte pin) {
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
11 this->pin = pin;
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
12 pinMode(this->pin, INPUT);
5
ea44b28e12ab 1.0.2 update - setup now resets the object
ark
parents: 2
diff changeset
13 this->isStopped = false;
ea44b28e12ab 1.0.2 update - setup now resets the object
ark
parents: 2
diff changeset
14 this->sum = 0.0;
ea44b28e12ab 1.0.2 update - setup now resets the object
ark
parents: 2
diff changeset
15 this->avg = 0.0;
ea44b28e12ab 1.0.2 update - setup now resets the object
ark
parents: 2
diff changeset
16 this->num = 0;
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
17 }
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
18
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
19 void Potentiometer::update() {
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
20 int v = analogRead(this->pin);
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
21 // if value is within jitter threshold of current value, add to average
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
22 int diff = abs(this->value - v);
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
23 if(this->num == 0 || diff <= Potentiometer::JITTER_THRESHOLD) {
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
24 if(this->num == Potentiometer::MAX_SAMPLES) {
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
25 this->sum -= this->avg;
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
26 this->num--;
1
c74204ea535c added isStopped
ark
parents: 0
diff changeset
27 this->isStopped = true;
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
28 }
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
29 this->sum += v;
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
30 this->num++;
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
31 this->avg = this->sum/this->num;
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
32 this->value = (int)(this->avg+.5);
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
33 this->ignore = false;
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
34 }
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
35 // otherwise
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
36 else {
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
37 // ignore one value above jitter
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
38 if(!this->ignore) {
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
39 this->ignore = true;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
40 }
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
41 // otherwise, if already ignored one spike, reset average
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
42 else {
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
43 this->value = v;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
44 this->sum = v;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
45 this->avg = v;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
46 this->num = 1;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
47 this->isStopped = false;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
48 this->ignore = false;
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
49 }
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
50 }
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
51 #ifdef DEBUG
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
52 Serial.print("pin=");
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
53 Serial.print(pin);
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
54 Serial.print(", sum=");
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
55 Serial.print(sum);
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
56 Serial.print(", num=");
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
57 Serial.print(num);
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
58 Serial.print(", avg=");
2
a5e06e87f081 adjustments
ark
parents: 1
diff changeset
59 Serial.print(avg);
a5e06e87f081 adjustments
ark
parents: 1
diff changeset
60 Serial.print(", v=");
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
61 Serial.print(v);
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
62 Serial.print(", diff=");
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
63 Serial.print(diff);
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
64 Serial.print(", ignore=");
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
65 Serial.print(ignore);
1
c74204ea535c added isStopped
ark
parents: 0
diff changeset
66 Serial.print(", isStopped=");
6
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
67 Serial.print(this->isStopped);
4e32bc18a7f7 adjusted jitter threshold, added ignore heuristic
ark
parents: 5
diff changeset
68 Serial.println("");
0
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
69 #endif
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
70 }
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
71
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
72
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
73 void Potentiometer::update(unsigned long ms) {
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
74 this->update();
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
75 }
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
76
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
77
4c32c9d4b94f initial, v1.0
ark
parents:
diff changeset
78