comparison Potentiometer.cpp @ 0:4c32c9d4b94f

initial, v1.0
author ark
date 2017-01-23 01:50 +0100
parents
children c74204ea535c
comparison
equal deleted inserted replaced
-1:000000000000 0:4c32c9d4b94f
1 #if defined(ARDUINO) && ARDUINO >= 100
2 #include "Arduino.h"
3 #else
4 #include "WProgram.h"
5 #endif
6
7 #include "Potentiometer.h"
8
9 /**
10 see Potentiometer.h for description.
11 */
12
13 Potentiometer::Potentiometer() {}
14
15 void Potentiometer::setup(byte pin) {
16 this->pin = pin;
17 pinMode(this->pin, INPUT);
18 }
19
20 void Potentiometer::update() {
21 int v = analogRead(this->pin);
22 // if value is within jitter threshold of current value, add to average
23 if(abs(this->avg - v) <= Potentiometer::JITTER_THRESHOLD) {
24 if(this->num == Potentiometer::MAX_SAMPLES) {
25 this->sum -= this->avg;
26 this->num--;
27 }
28 this->sum += v;
29 this->num++;
30 this->avg = this->sum/this->num;
31 this->value = (int)(this->avg+.5);
32 }
33 // otherwise reset average
34 else {
35 this->value = v;
36 this->sum = v;
37 this->avg = v;
38 this->num = 1;
39 }
40 #ifdef DEBUG
41 Serial.print("pin=");
42 Serial.print(pin);
43 Serial.print(", sum=");
44 Serial.print(sum);
45 Serial.print(", num=");
46 Serial.print(num);
47 Serial.print(", avg=");
48 Serial.println(avg);
49 #endif
50 }
51
52
53 void Potentiometer::update(unsigned long ms) {
54 this->update();
55 }
56
57
58