forked from HanzeRacingDivision/HRD05-pedalbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltering.h
43 lines (34 loc) · 1.22 KB
/
filtering.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
#pragma once
#include "./config.h"
/**
* Simple moving average over a number of samples
* Based on https://www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing
*
* @todo add a small delay between analogReads to prevent multiplexing issues
*/
class MovingAverage {
private:
uint8_t PIN = A0;
static const int BUFFER_SIZE = SENSOR_FILTER_SAMPLES;
long readings[BUFFER_SIZE];
long value = 0;
long sum = 0;
int idx = 0;
public:
long average = 0;
MovingAverage(const uint8_t analog_pin)
{
this->PIN = analog_pin;
for (int i = 0; i < this->BUFFER_SIZE; i++)
this->readings[i] = 0;
}
void update()
{
this->sum -= this->readings[idx]; // Remove the oldest entry from the sum
this->value = analogRead(this->PIN); // Read the next sensor value
this->sum += this->value; // Add the newest reading to the sum
this->readings[idx] = this->value; // Add the newest reading to the window
this->idx = (idx+1) % this->BUFFER_SIZE; // Increment the index, and wrap to 0 if it exceeds the window size
this->average = (this->sum / this->BUFFER_SIZE);
}
};