wobblyhead hg > arduino > Potentiometer
diff Potentiometer.cpp @ 0:4c32c9d4b94f
initial, v1.0
| author | ark |
|---|---|
| date | 2017-01-23 01:50 +0100 |
| parents | |
| children | c74204ea535c |
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(); +} + + +
