-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharp.ino
70 lines (62 loc) · 1.49 KB
/
harp.ino
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
void setup() {
Serial.begin(9600);
calibrate();
}
float analogPins[] = {0, 1, 2, 3, 4, 5};
int numAnalogPins = 6;
int thresholds[6];
float percentThresh = 1.03;
int absoluteThresh = 20;
int sensorHistory[6][10];
int bufferIndex = 0;
void calibrate() {
int maxSteps = 10;
for (int curStep = 0; curStep < maxSteps; curStep++) {
for (int pin = 0; pin < numAnalogPins; pin++) {
thresholds[pin] += analogRead(analogPins[pin]);
}
}
for (int pin = 0; pin < numAnalogPins; pin++) {
thresholds[pin] = thresholds[pin] / maxSteps;
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 10; j++) {
sensorHistory[i][j] = thresholds[i];
}
}
}
void loop() {
for (int i = 0; i < numAnalogPins; i++) {
int val = analogRead(analogPins[i]);
//Serial.println(val);
int oldAvg = 0;
for (int j = 0; j < 10; j++) {
oldAvg += sensorHistory[i][j];
}
oldAvg = oldAvg / 10;
// Serial.print(oldAvg);
// Serial.print(";");
// Serial.print(val);
// Serial.print(";");
// Serial.print(bufferIndex);
// Serial.println();
if (val > oldAvg + absoluteThresh) {
Serial.print(1);
}
else {
Serial.print(0);
}
sensorHistory[i][bufferIndex] = val;
//if (val > thresholds[i] * percentThresh) {
// if (val > thresholds[i] + absoluteThresh) {
// Serial.print(1);
// }
//
// else {
// Serial.print(0);
// }
}
bufferIndex = (bufferIndex + 1) % 10;
Serial.println();
delay(40);
}