-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_operator.ino.ino
More file actions
81 lines (68 loc) · 2.31 KB
/
max_operator.ino.ino
File metadata and controls
81 lines (68 loc) · 2.31 KB
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
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
#include "spo2_algorithm.h"
MAX30105 particleSensor;
const byte AVG_SAMPLES = 4; // Number of samples for averaging
float bpmSamples[AVG_SAMPLES];
int spo2Samples[AVG_SAMPLES];
byte sampleIndex = 0;
bool bufferFull = false;
long lastBeat = 0;
float beatsPerMinute;
void setup() {
Serial.begin(115200);
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 was not found. Please check wiring/power.");
while (1);
}
// Setup sensor with recommended SpO2 and BPM settings
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A); // Low amplitude for detection
particleSensor.setPulseAmplitudeGreen(0);
Serial.println("Place your finger on the sensor...");
}
void loop() {
long irValue = particleSensor.getIR();
// If a finger is detected (IR value > 50,000)
if (irValue > 50000) {
if (checkForBeat(irValue) == true) {
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
// Basic filtering: only record realistic human heart rates
if (beatsPerMinute < 255 && beatsPerMinute > 20) {
bpmSamples[sampleIndex] = beatsPerMinute;
// Simulating SpO2 for simplified code; use spo2_algorithm.h for full accuracy
// Note: Real SpO2 requires the red and IR buffers (approx 4s of data)
spo2Samples[sampleIndex] = 98; // Placeholder: replace with algorithm result
sampleIndex++;
if (sampleIndex >= AVG_SAMPLES) {
sampleIndex = 0;
bufferFull = true;
}
// Calculate and display averages after 20 samples
if (bufferFull) {
float avgBPM = 0;
float avgSPO2 = 0;
for (int i = 0; i < AVG_SAMPLES; i++) {
avgBPM += bpmSamples[i];
avgSPO2 += spo2Samples[i];
}
avgBPM /= AVG_SAMPLES;
avgSPO2 /= AVG_SAMPLES;
Serial.print("--- AVG (20 Samples) --- ");
Serial.print("BPM: "); Serial.print(avgBPM);
Serial.print(" | SpO2: "); Serial.print(avgSPO2);
Serial.println("%");
}
}
}
} else {
// Reset buffer if finger is removed
sampleIndex = 0;
bufferFull = false;
Serial.println("No finger detected.");
delay(500);
}
}