-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPC.ino
281 lines (247 loc) · 9.49 KB
/
BPC.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Pin Definitions and Constants
const int signalPin = A1; // Signal pin for oscillometric signal
const int pressurePin = A0; // Pressure sensor pin
const int buttonPin = 2; // Pushbutton pin
const int TransistorGate = 13; // Transistor gate pin for cuff control
const float pressureThreshold = 3.45; // Start tracking when pressure voltage > 3V with .35v buffer
const float peakThreshold = 3.0; // Voltage above which we start tracking a peak
const int sampleRate = 10; // Sample rate in milliseconds
const int maxReadings = 100; // Maximum number of readings
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
// Variables for Transistor Control
int inflationState = LOW; // Current state of the transistor
int buttonState; // Current reading from the input pin
int lastButtonState = LOW; // Previous reading from the input pin
unsigned long lastDebounceTime = 0;// Last time the button state changed
// Variables for Pulse Tracking
unsigned long currentTime;
bool tracking = false; // Are we currently tracking pulses?
unsigned long trackingStartTime = 0; // Time when tracking started
int pulseCount = 0;
float pressureReadings[maxReadings]; // Array to store pressure readings in mmHg
float pulseAmplitudes[maxReadings]; // Array to store pulse amplitudes
unsigned long pulseTimes[maxReadings]; // Array to store pulse times
// Variables for Oscillometric Pulse
bool trackingPulse = false;
bool lookingForPeak = false;
bool lookingForValley = false;
float peakValue = 0.0;
float valleyValue = 5.0; // Initialize to max voltage to find the minimum
unsigned long lastPeakTime = 0;
// Variables for Signal Filtering
const int filterSize = 5;
float signalBuffer[filterSize];
int bufferIndex = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(TransistorGate, OUTPUT);
// Set initial transistor state
digitalWrite(TransistorGate, LOW);
}
void loop() {
currentTime = millis();
float pressureVoltage = analogRead(pressurePin) * (5.0 / 1023.0);
// Convert pressure voltage to pressure in mmHg
float pressure_mmHg = 50.0 * pressureVoltage; // 50 mmHg per volt
// Transistor Control Logic
if (!tracking) {
int reading = digitalRead(buttonPin);
// Debounce logic
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// Toggle inflation state if button is pressed
if (buttonState == HIGH) {
inflationState = !inflationState;
digitalWrite(TransistorGate, inflationState);
}
}
}
lastButtonState = reading;
// Stop inflation if pressure exceeds threshold and start tracking pulses
if (pressureVoltage > pressureThreshold && inflationState == HIGH) {
inflationState = LOW;
digitalWrite(TransistorGate, LOW);
tracking = true;
trackingStartTime = currentTime; // Record the time when tracking started
pulseCount = 0; // Reset pulse count
Serial.println("Started tracking pulses...");
}
}
// Stop tracking when pressure voltage drops below a certain threshold (e.g., 1V)
if (tracking && pressureVoltage < 1.0) {
tracking = false;
Serial.println("Stopped tracking pulses.");
processResults();
}
if (tracking && (currentTime - trackingStartTime >= 3000)) {
// Read oscillometric signal after 3-second delay
float rawSignalVoltage = analogRead(signalPin) * (5.0 / 1023.0);
float signalVoltage = getFilteredSignal(rawSignalVoltage);
// Pulse detection logic
if (!trackingPulse) {
// Not currently tracking a pulse
if (signalVoltage > peakThreshold) {
// Signal voltage is above the peak threshold, start tracking pulse
trackingPulse = true;
lookingForPeak = true;
peakValue = signalVoltage;
lastPeakTime = currentTime;
}
} else {
// Currently tracking a pulse
if (lookingForPeak) {
if (signalVoltage > peakValue) {
// Still ascending towards the peak
peakValue = signalVoltage;
lastPeakTime = currentTime;
} else {
// Signal voltage decreased, peak has been found
lookingForPeak = false;
lookingForValley = true;
valleyValue = signalVoltage;
}
} else if (lookingForValley) {
if (signalVoltage < valleyValue) {
// Still descending towards the valley
valleyValue = signalVoltage;
} else {
// Signal voltage increased, valley has been found
// Pulse is over
lookingForValley = false;
// Calculate pulse amplitude
float pulseAmplitude = peakValue - valleyValue;
// Only record if amplitude is greater than 0.4
if (pulseAmplitude > 0.4) {
// Record the amplitude and corresponding pressure
if (pulseCount < maxReadings) {
pulseAmplitudes[pulseCount] = pulseAmplitude;
pressureReadings[pulseCount] = pressure_mmHg; // Store pressure in mmHg
pulseTimes[pulseCount] = lastPeakTime;
pulseCount++;
}
} else {
// Ignore pulses with amplitude <= 0.4
Serial.println("Pulse amplitude too low, ignoring.");
}
// Reset peak and valley values
peakValue = 0.0;
valleyValue = 5.0;
trackingPulse = false;
}
}
}
}
delay(sampleRate); // Delay for sampling rate
}
float getFilteredSignal(float newSignal) {
signalBuffer[bufferIndex] = newSignal;
bufferIndex = (bufferIndex + 1) % filterSize;
float sum = 0;
for (int i = 0; i < filterSize; i++) {
sum += signalBuffer[i];
}
return sum / filterSize;
}
void processResults() {
// Find the maximum pulse amplitude and its index
float maxAmplitude = 0;
int maxIndex = 0;
for (int i = 0; i < pulseCount; i++) {
if (pulseAmplitudes[i] > maxAmplitude) {
maxAmplitude = pulseAmplitudes[i];
maxIndex = i;
}
}
// Check if we have enough data
if (maxAmplitude == 0) {
Serial.println("No valid pulses detected.");
return;
}
// Adjusted thresholds based on empirical data
float systolicThreshold = 0.55 * maxAmplitude;
float diastolicThreshold = 0.47 * maxAmplitude;
float systolicPressure = 0;
float diastolicPressure = 0;
// Find systolic pressure (before max amplitude)
for (int i = maxIndex; i >= 0; i--) {
if (pulseAmplitudes[i] <= systolicThreshold) {
if (i < maxIndex) {
// Linear interpolation between the two points
float x1 = pulseAmplitudes[i];
float x2 = pulseAmplitudes[i + 1];
float y1 = pressureReadings[i];
float y2 = pressureReadings[i + 1];
systolicPressure = y1 + (systolicThreshold - x1) * (y2 - y1) / (x2 - x1);
} else {
systolicPressure = pressureReadings[i];
}
break;
}
}
// Find diastolic pressure (after max amplitude)
for (int i = maxIndex; i < pulseCount; i++) {
if (pulseAmplitudes[i] <= diastolicThreshold) {
if (i > maxIndex) {
// Linear interpolation between the two points
float x1 = pulseAmplitudes[i - 1];
float x2 = pulseAmplitudes[i];
float y1 = pressureReadings[i - 1];
float y2 = pressureReadings[i];
diastolicPressure = y1 + (diastolicThreshold - x1) * (y2 - y1) / (x2 - x1);
} else {
diastolicPressure = pressureReadings[i];
}
break;
}
}
// If systolicPressure is still zero, use first detected pulse
if (systolicPressure == 0 && maxIndex > 0) {
systolicPressure = pressureReadings[0];
Serial.println("Systolic pressure estimated from first detected pulse.");
}
// If diastolicPressure is still zero, use last detected pulse
if (diastolicPressure == 0 && maxIndex < pulseCount - 1) {
diastolicPressure = pressureReadings[pulseCount - 1];
Serial.println("Diastolic pressure estimated from last detected pulse.");
}
// Calculate Mean Arterial Pressure (MAP)
float mapPressure = pressureReadings[maxIndex];
// Calculate BPM
float bpm = 0;
if (pulseCount >= 2) {
float totalInterval = 0;
for (int i = 1; i < pulseCount; i++) {
totalInterval += (pulseTimes[i] - pulseTimes[i - 1]);
}
float averageInterval = totalInterval / (pulseCount - 1);
bpm = 60000.0 / averageInterval;
} else {
Serial.println("Not enough pulses detected for BPM calculation.");
}
// Print results
Serial.println("Measurement Results:");
Serial.print("Systolic Pressure: ");
Serial.print(systolicPressure);
Serial.println(" mmHg");
Serial.print("Diastolic Pressure: ");
Serial.print(diastolicPressure);
Serial.println(" mmHg");
Serial.print("MAP Pressure: ");
Serial.print(mapPressure);
Serial.println(" mmHg");
Serial.print("Calculated BPM: ");
Serial.println(bpm);
// Print pulse amplitude and pressure readings for analysis
Serial.println("Pulse Amplitude vs. Pressure Readings:");
for (int i = 0; i < pulseCount; i++) {
Serial.print("Pressure: ");
Serial.print(pressureReadings[i]);
Serial.print(" mmHg, Amplitude: ");
Serial.println(pulseAmplitudes[i]);
}
}