-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmooth.cpp
More file actions
35 lines (27 loc) · 706 Bytes
/
Smooth.cpp
File metadata and controls
35 lines (27 loc) · 706 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
/**
Based on https://www.arduino.cc/en/Tutorial/Smoothing
Smoothing
Reads repeatedly from an analog input, calculating a running average and
printing it to the computer. Keeps twenty readings in an array and continually
averages them.
*/
#include "Smooth.h"
Smooth::Smooth() {
for (int r = 0; r < MAX_READINGS; r++) {
readings[r] = 0;
}
}
float Smooth::putReading(int reading) {
total = total - readings[readIndex];
readings[readIndex] = reading;
total = total + reading;
readIndex = readIndex + 1;
if (readIndex >= MAX_READINGS) {
readIndex = 0;
}
average = (float) total / (float) MAX_READINGS;
return average;
}
float Smooth::getAverage() {
return average;
}