-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino.ino
103 lines (86 loc) · 3.19 KB
/
arduino.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const int GSR = A0;
const int SAMPLES = 10;
int readings[SAMPLES];
int sensorValue = 0;
int gsr_average = 0;
bool isRunning = false;
void setup() {
Serial.begin(9600);
Serial.println("Press 's' to start/stop data collection");
}
void loop() {
if (Serial.available() > 0) {
char input = Serial.read();
if (input == 's' || input == 'S') {
// Take one final/initial reading when toggling
for (int i = 0; i < SAMPLES; i++) {
readings[i] = analogRead(GSR);
delay(5);
}
// Sort the readings
for (int i = 0; i < SAMPLES - 1; i++) {
for (int j = 0; j < SAMPLES - i - 1; j++) {
if (readings[j] > readings[j + 1]) {
int temp = readings[j];
readings[j] = readings[j + 1];
readings[j + 1] = temp;
}
}
}
int medianGSR = readings[SAMPLES / 2];
int human_resistance = 0;
if (516 - medianGSR != 0) {
human_resistance = abs(((1024 + 2 * medianGSR) * 10000) / (516 - medianGSR));
}
isRunning = !isRunning;
if (!isRunning) {
Serial.print("Final Median Resistance: ");
Serial.println(human_resistance);
} else {
Serial.println("Starting data collection...");
}
}
}
// Only collect and process data if isRunning is true
if (isRunning) {
// Collect readings into array instead of immediate averaging
for (int i = 0; i < SAMPLES; i++) {
readings[i] = analogRead(GSR);
delay(50);
}
// Sort the array (bubble sort since it's a small dataset)
for (int i = 0; i < SAMPLES - 1; i++) {
for (int j = 0; j < SAMPLES - i - 1; j++) {
if (readings[j] > readings[j + 1]) {
int temp = readings[j];
readings[j] = readings[j + 1];
readings[j + 1] = temp;
}
}
}
// Calculate Q1, Q2 (median), and Q3
int Q1 = readings[SAMPLES / 4];
int Q2 = readings[SAMPLES / 2]; // median
int Q3 = readings[3 * SAMPLES / 4];
// Calculate IQR and bounds for outlier detection
int IQR = Q3 - Q1;
int lowerBound = Q1 - (1.5 * IQR);
int upperBound = Q3 + (1.5 * IQR);
// Count valid readings (non-outliers)
int validCount = 0;
long validSum = 0;
for (int i = 0; i < SAMPLES; i++) {
if (readings[i] >= lowerBound && readings[i] <= upperBound) {
validSum += readings[i];
validCount++;
}
}
// Calculate final GSR average excluding outliers
gsr_average = validCount > 0 ? validSum / validCount : Q2;
int human_resistance = 0;
if (516 - gsr_average != 0) {
human_resistance = abs(((1024 + 2 * gsr_average) * 10000) / (516 - gsr_average));
}
}
delay(50); // Delay for readability in plotter
}