-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patharduino.ino
339 lines (271 loc) · 7.69 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// -------------------------------------- Parameters ------------------------------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int analogInPin = A0;
int reading = 0;
int BPM = 0;
float difference=0;
float avg;
unsigned long Ttime=0;
//bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
int count=0;
float maximum=0.0;
float minimum=400.0;
unsigned long Stime=0;
unsigned long Rtime=0;
float Rvalue;
float Svalue;
unsigned long qrstime;
bool R=true;
bool S=false;
bool T=false;
bool print1;
/*int ledPin = 50;
int ledPin2 = 48;
int ledPin3 = 46;
int ledPin4 = 44;
int ledPin5 = 13;*/
int beat=0;
float UpperThreshold=210;
float LowerThreshold=205;
bool IgnoreReading = false;
unsigned long end_time;
// Originally collected signal (x, x1 and x2), notch filtered (y, y1, y2 and y3) and notch & low-pass filtered ECG signal (z, z1, z2 and z3).
// x = x(t); x1 = x(t-1); x1 = x(t-2). The same applies for y and z.
volatile double x = 0.0;
volatile double x1 = 0.0;
volatile double x2 = 0.0;
volatile double y = 0.0;
volatile double y1 = 0.0;
volatile double y2 = 0.0;
volatile double y3 = 0.0;
volatile double z = 0.0;
volatile double z1 = 0.0;
volatile double z2 = 0.0;
volatile double z3 = 0.0;
String bpm = "Normal Heart Rate";
double maxi;
double maxi2;
double mini;
double sum;
// Flag variables.
int aux = 0;
int aux2 = 0;
int aux3 = 0;
double auxx;
// Iterative variable.
int ii = 0;
int k = 0;
int l;
// Arrays
double timeVecto2 [512]; // Array which keeps 512 samples from the ECG signal (more or less, one cycle: sampling frequency = 500 Hz)
double timeVectorHigh2 [60]; // Saves 30 different instants (in ms) which corresponds to each R peak in the signal.
double timeVectorHigh3 [60]; // Time duration between 30 different R peaks.
double timeVectorLow2 [60]; // Saves 30 different instants (in ms) which corresponds to each S peak in the signal.
double timeVector2Medium [60]; // Saves 30 different instants (in ms) which corresponds to each T peak in the signal.
double rt [60]; // 30 different RT intervals from the ECG signal.
double rs[60]; // 30 different RS intervals from the ECG signal.
double st[60]; // 30 different ST intervals from the ECG signal.
double rs2;
double st2;
double rsf;
double stf;
double res1;
double res2;
double resf;
double maxx = 0;
double minn = 10000;
// Defining combination of on and off pins to show each number.
const int numeral[10] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// Arduino pins used to connect all the 7 segments + dots.
//const int segmentPins[] = { 22, 7, 8, 6, 5, 3, 2, 9};
// Number of digits allowed in the display.
//const int numberofDigits = 4;
// Arduino pins used to connect all the 4 digits.
//const int digitPins[numberofDigits] = {10, 11, 12, 4};
// -------------------------------------------------
void setup() {
// put your setup code here, to run once:
pinMode(A1, OUTPUT);
lcd.begin(16,2);
lcd.print("BPM");
Serial.begin(9600);
end_time=millis()+10000;
/* pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
for (int i = 0; i < 8; i++)
pinMode(segmentPins[i], OUTPUT); //set segment and DP pins to output
//sets the digit pins as outputs
for (int i = 0; i < numberofDigits; i++)
pinMode(digitPins[i], OUTPUT);*/
cli(); //stop interrupts
//set timer1 interrupt at 500Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1Hz increments
OCR1A = 3999;// = (16*10^6) / (8*500) - 1 (must be <65536)
//OCR1A = 6666 ;// = (16*10^6) / (8*300) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bits for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interrupts
}
ISR(TIMER1_COMPA_vect) { // commands executed at each time interrupt
// -------------------------------------------- Acquiring Data ------------------------------------------
x = analogRead(analogInPin);
// ----------------------------------------------- Filtering --------------------------------------------
// Filtering around 40 and 60 Hz (notch).
// y = 0.2012 * x - 0.3256 * x1 + 0.2012 * x2 + 0.3256 * y1 + 0.5975 * y2;
//y = 0.5578 * x - 0.9025 * x1 + 0.5578 * x2 + 0.9025 * y1 - 0.1156 * y2;
//Filtering around 49Hz and 51Hz (notch -500Hz).
y = 0.7161 * x - 1.1587 * x1 + 0.7161 * x2 + 1.1587 * y1 - 0.4323 * y2;
//Filtering around 49Hz and 51Hz (notch -300Hz)
//y = 0.6022 * x - 0.6022 * x1 + 0.6022 * x2 + 0.6022 * y1 - 0.2043 * y2;
// Low-pass Filter 100 Hz.
z = 0.0495 * y + 0.1486 * y1 + 0.1486 * y2 + 0.0495 * y3 + 1.1619 * z1 - 0.6959 * z2 + 0.1378 * z3;
x2 = x1;
x1 = x;
y3 = y2;
y2 = y1;
y1 = y;
z3 = z2;
z2 = z1;
z1 = z;
// Saving z value
timeVecto2[k] = z;
// timeVecto2[k] = y;
k = (k + 1) % 512;
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.print(" ");
float voltage=5 * 100 * z / 1023;
analogWrite(A1,voltage);
/*if(millis()>=end_time){
lcd.clear();
lcd.print("BPM");
lcd.setCursor(0,1);
lcd.print(beat*6);
beat=0;
end_time=millis()+10000; //reset the timer
}*/
/*if( voltage > UpperThreshold && R==true)
{
if(voltage>maximum)
{
maximum=voltage;
}
else
{
Rtime=millis();
maximum=0;
R=false;
S=true;
}
}*/
if(voltage > UpperThreshold && IgnoreReading == false){
//beat=beat+1;
if(FirstPulseDetected == false){
FirstPulseTime = millis();
FirstPulseDetected = true;
}
else{
SecondPulseTime = millis();
PulseInterval = SecondPulseTime - FirstPulseTime;
FirstPulseTime = SecondPulseTime;
BPM = (1.0/PulseInterval) * 60.0 * 1000;
lcd.clear();
lcd.print("BPM");
lcd.setCursor(0,1);
lcd.print(BPM);
}
IgnoreReading = true;
}
if(voltage < LowerThreshold){
IgnoreReading = false;
}
/*if(voltage<LowerThreshold && S==true)
{
if(voltage<minimum)
{
minimum=voltage;
}
else
{
Stime=millis();
Svalue=minimum;
//lcd.setCursor(5,0);
//lcd.print(2*(Stime-Rtime));
minimum=400;
R=true;
S=false;
T=true;
}
}
if(T==true)
{
if(voltage-Svalue>6)
{
Ttime=millis();
difference=2*(Ttime-Rtime);
avg=avg +difference;
count=count+1;
if(count==5)
{
count=0;
lcd.setCursor(5,0);
lcd.print(avg/5);
}
T=false;
}
}
/*if(R==false){
if(voltage<minimum)
{
minimum=voltage;
Stime=millis();
}
else{
if(print1==true){lcd.setCursor(10,0);
lcd.print(Stime/1000);
lcd.setCursor(5,0);
lcd.print(Rtime/1000);
print1=false;}
}
}*/
//Serial.write((byte)voltage/20);
//Serial.print(voltage);
//count=count+1;
//Serial.print(Svalue);
// Serial.print(Rvalue);
Serial.print(voltage/20);
//Serial.print('\t');
//Serial.print(millis());
//Serial.println();
// Serial.print(',');
//Serial.print(millis());
Serial.println();
//if (count == 10000) Serial.print('e');
}