-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoti.cpp
More file actions
40 lines (31 loc) · 786 Bytes
/
Copy pathPoti.cpp
File metadata and controls
40 lines (31 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Poti.cxx
#include "Poti.h"
#include <Arduino.h>
Poti::Poti() {
AnalogPin = -1;
}
void Poti::attach(const int anAnalogPin) {
AnalogPin = anAnalogPin;
pinMode(AnalogPin, INPUT);
NumReadings = 10;
readings = new int[NumReadings];
for (int i=0; i<NumReadings; i++) {
readings[i] = 0;
}
total = 0;
readIndex = 0;
}
int Poti::analogRead() {
if (-1 == AnalogPin) return -1;
int newValue = ::analogRead(AnalogPin); // read new value
newValue = constrain(newValue, 0, 1023);
total = total - readings[readIndex];
readings[readIndex] = newValue;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= NumReadings) {
readIndex = 0; // wrap around to the beginning
}
double average = total / NumReadings;
return average;
}