# HG changeset patch # User ark # Date 1499034269 -7200 # Node ID 4e32bc18a7f7c7b1a0c1d81bee3df6da82a738dd # Parent ea44b28e12ab5753bc7a9027df1f0bae1fc1b664 adjusted jitter threshold, added ignore heuristic diff -r ea44b28e12ab -r 4e32bc18a7f7 Potentiometer.cpp --- a/Potentiometer.cpp Thu Jun 22 01:42:31 2017 +0200 +++ b/Potentiometer.cpp Mon Jul 03 00:24:29 2017 +0200 @@ -1,7 +1,6 @@ #include "Arduino.h" #include "Potentiometer.h" - /** see Potentiometer.h for description. */ @@ -20,7 +19,8 @@ void Potentiometer::update() { int v = analogRead(this->pin); // if value is within jitter threshold of current value, add to average - if(abs(this->avg - v) <= Potentiometer::JITTER_THRESHOLD) { + int diff = abs(this->value - v); + if(this->num == 0 || diff <= Potentiometer::JITTER_THRESHOLD) { if(this->num == Potentiometer::MAX_SAMPLES) { this->sum -= this->avg; this->num--; @@ -30,14 +30,23 @@ this->num++; this->avg = this->sum/this->num; this->value = (int)(this->avg+.5); + this->ignore = false; } - // otherwise reset average + // otherwise else { - this->value = v; - this->sum = v; - this->avg = v; - this->num = 1; - this->isStopped = false; + // ignore one value above jitter + if(!this->ignore) { + this->ignore = true; + } + // otherwise, if already ignored one spike, reset average + else { + this->value = v; + this->sum = v; + this->avg = v; + this->num = 1; + this->isStopped = false; + this->ignore = false; + } } #ifdef DEBUG Serial.print("pin="); @@ -49,9 +58,14 @@ Serial.print(", avg="); Serial.print(avg); Serial.print(", v="); - Serial.print(value); + Serial.print(v); + Serial.print(", diff="); + Serial.print(diff); + Serial.print(", ignore="); + Serial.print(ignore); Serial.print(", isStopped="); - Serial.println(this->isStopped); + Serial.print(this->isStopped); + Serial.println(""); #endif } diff -r ea44b28e12ab -r 4e32bc18a7f7 Potentiometer.h --- a/Potentiometer.h Thu Jun 22 01:42:31 2017 +0200 +++ b/Potentiometer.h Mon Jul 03 00:24:29 2017 +0200 @@ -42,7 +42,7 @@ * added to current average. value changes larger than the threshold * reset the average. */ - const unsigned long JITTER_THRESHOLD = 3; + const unsigned long JITTER_THRESHOLD = 15; /** max samples for de-jittering. */ @@ -53,6 +53,7 @@ float sum; float avg; int num; + bool ignore; }; diff -r ea44b28e12ab -r 4e32bc18a7f7 library.properties --- a/library.properties Thu Jun 22 01:42:31 2017 +0200 +++ b/library.properties Mon Jul 03 00:24:29 2017 +0200 @@ -1,5 +1,5 @@ name=Potentiometer -version=1.0.2 +version=1.0.3 author=Arkadiusz Juszczyk maintainer=Arkadiusz Juszczyk sentence=Dejittered potentiometer for Arduino