wobblyhead hg > arduino > Potentiometer
comparison Potentiometer.cpp @ 6:4e32bc18a7f7 default tip
adjusted jitter threshold, added ignore heuristic
| author | ark |
|---|---|
| date | 2017-07-03 00:24 +0200 |
| parents | ea44b28e12ab |
| children |
comparison
equal
deleted
inserted
replaced
| 5:ea44b28e12ab | 6:4e32bc18a7f7 |
|---|---|
| 1 #include "Arduino.h" | 1 #include "Arduino.h" |
| 2 #include "Potentiometer.h" | 2 #include "Potentiometer.h" |
| 3 | |
| 4 | 3 |
| 5 /** | 4 /** |
| 6 see Potentiometer.h for description. | 5 see Potentiometer.h for description. |
| 7 */ | 6 */ |
| 8 | 7 |
| 18 } | 17 } |
| 19 | 18 |
| 20 void Potentiometer::update() { | 19 void Potentiometer::update() { |
| 21 int v = analogRead(this->pin); | 20 int v = analogRead(this->pin); |
| 22 // if value is within jitter threshold of current value, add to average | 21 // if value is within jitter threshold of current value, add to average |
| 23 if(abs(this->avg - v) <= Potentiometer::JITTER_THRESHOLD) { | 22 int diff = abs(this->value - v); |
| 23 if(this->num == 0 || diff <= Potentiometer::JITTER_THRESHOLD) { | |
| 24 if(this->num == Potentiometer::MAX_SAMPLES) { | 24 if(this->num == Potentiometer::MAX_SAMPLES) { |
| 25 this->sum -= this->avg; | 25 this->sum -= this->avg; |
| 26 this->num--; | 26 this->num--; |
| 27 this->isStopped = true; | 27 this->isStopped = true; |
| 28 } | 28 } |
| 29 this->sum += v; | 29 this->sum += v; |
| 30 this->num++; | 30 this->num++; |
| 31 this->avg = this->sum/this->num; | 31 this->avg = this->sum/this->num; |
| 32 this->value = (int)(this->avg+.5); | 32 this->value = (int)(this->avg+.5); |
| 33 this->ignore = false; | |
| 33 } | 34 } |
| 34 // otherwise reset average | 35 // otherwise |
| 35 else { | 36 else { |
| 36 this->value = v; | 37 // ignore one value above jitter |
| 37 this->sum = v; | 38 if(!this->ignore) { |
| 38 this->avg = v; | 39 this->ignore = true; |
| 39 this->num = 1; | 40 } |
| 40 this->isStopped = false; | 41 // otherwise, if already ignored one spike, reset average |
| 42 else { | |
| 43 this->value = v; | |
| 44 this->sum = v; | |
| 45 this->avg = v; | |
| 46 this->num = 1; | |
| 47 this->isStopped = false; | |
| 48 this->ignore = false; | |
| 49 } | |
| 41 } | 50 } |
| 42 #ifdef DEBUG | 51 #ifdef DEBUG |
| 43 Serial.print("pin="); | 52 Serial.print("pin="); |
| 44 Serial.print(pin); | 53 Serial.print(pin); |
| 45 Serial.print(", sum="); | 54 Serial.print(", sum="); |
| 47 Serial.print(", num="); | 56 Serial.print(", num="); |
| 48 Serial.print(num); | 57 Serial.print(num); |
| 49 Serial.print(", avg="); | 58 Serial.print(", avg="); |
| 50 Serial.print(avg); | 59 Serial.print(avg); |
| 51 Serial.print(", v="); | 60 Serial.print(", v="); |
| 52 Serial.print(value); | 61 Serial.print(v); |
| 62 Serial.print(", diff="); | |
| 63 Serial.print(diff); | |
| 64 Serial.print(", ignore="); | |
| 65 Serial.print(ignore); | |
| 53 Serial.print(", isStopped="); | 66 Serial.print(", isStopped="); |
| 54 Serial.println(this->isStopped); | 67 Serial.print(this->isStopped); |
| 68 Serial.println(""); | |
| 55 #endif | 69 #endif |
| 56 } | 70 } |
| 57 | 71 |
| 58 | 72 |
| 59 void Potentiometer::update(unsigned long ms) { | 73 void Potentiometer::update(unsigned long ms) { |
