wobblyhead hg > arduino > Potentiometer
changeset 0:4c32c9d4b94f
initial, v1.0
| author | ark |
|---|---|
| date | 2017-01-23 01:50 +0100 |
| parents | |
| children | c74204ea535c |
| files | Potentiometer.cpp Potentiometer.h library.properties |
| diffstat | 3 files changed, 123 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Potentiometer.cpp Mon Jan 23 01:50:42 2017 +0100 @@ -0,0 +1,58 @@ +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#include "Potentiometer.h" + +/** + see Potentiometer.h for description. +*/ + +Potentiometer::Potentiometer() {} + +void Potentiometer::setup(byte pin) { + this->pin = pin; + pinMode(this->pin, INPUT); +} + +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) { + if(this->num == Potentiometer::MAX_SAMPLES) { + this->sum -= this->avg; + this->num--; + } + this->sum += v; + this->num++; + this->avg = this->sum/this->num; + this->value = (int)(this->avg+.5); + } + // otherwise reset average + else { + this->value = v; + this->sum = v; + this->avg = v; + this->num = 1; + } + #ifdef DEBUG + Serial.print("pin="); + Serial.print(pin); + Serial.print(", sum="); + Serial.print(sum); + Serial.print(", num="); + Serial.print(num); + Serial.print(", avg="); + Serial.println(avg); + #endif +} + + +void Potentiometer::update(unsigned long ms) { + this->update(); +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Potentiometer.h Mon Jan 23 01:50:42 2017 +0100 @@ -0,0 +1,56 @@ +#ifndef Potentiometer_h +#define Potentiometer_h + +/** + utility class for dejittering a potentiometer and/or mapping its value to + ranges. + + (c) 2017 arkadiusz@wobblyhead.net +*/ +class Potentiometer { + public: + // dejittered pot value + int value; + + /** + create an uninitialised Potentiometer. + setup() must be called after this to make it usable. + */ + Potentiometer(); + + /** + setup pod instance. + @param {byte} pin input pin + */ + void setup(byte pin); + + /** + check pin and update values. + */ + void update(); + + /** + check pin and update values. + @param {unsigned long} ms ignored + */ + void update(unsigned long ms); + + /** jitter threshold. value changes smalled than the threshold will be + * added to current average. value changes larger than the threshold + * reset the average. + */ + const unsigned long JITTER_THRESHOLD = 2; + + /** max samples for de-jittering. + */ + const int MAX_SAMPLES = 30; + + private: + byte pin; + float sum; + float avg; + int num; + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/library.properties Mon Jan 23 01:50:42 2017 +0100 @@ -0,0 +1,9 @@ +name=Potentiometer +version=1.0 +author=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net> +maintainer=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net> +sentence=Dejittered potentiometer for Arduino +paragraph=Encapsulates de-jittered potentiometer +category=Signal Input/Output +url=https://hg.wobblyhead.net/arduino/Potentiometer +architectures=*
