-
Notifications
You must be signed in to change notification settings - Fork 0
/
averager.h
47 lines (39 loc) · 751 Bytes
/
averager.h
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
41
42
43
44
45
46
47
/* Averager
created 2015
by A. Heidgerken-Greene, Science Museum of MN
*/
#include "arduino.h"
class averager {
public:
// variables for averaging.
int numSamps;
double * samps;
int pntr;
double tot;
double ave;
averager(int num = 10){// init the averaging variables.
pntr=tot=ave=0;
numSamps = num;
samps = new double[numSamps];
for(int i=0; i<numSamps; i++){
samps[i]=0;
tot+=samps[i];
}
}
double operator()(){
return ave;
}
void reset(){
for(int i=0; i<numSamps; i++){
samps[i]=0;
}
tot = ave = 0;
}
void idle(double newRead){
tot-=samps[pntr];
samps[pntr]=newRead;
tot+=newRead;
ave = tot/numSamps;
pntr=(pntr + 1)%numSamps;
}
};