-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1.txt
76 lines (71 loc) · 2.29 KB
/
Project1.txt
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
/*Code written by RajenChhetry _ Version 2.0 _ Detection of heart rate in BMP and display of ECG signal
Assam University(TSSOT)
email : [email protected] */
int UpperThreshold = 518;
int LowerThreshold = 490;
int reading = 0;
float BPM = 0.0;
bool IgnoreReading = false; //bool(data type) holds one of two values_true or false
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
const unsigned long delayTime = 10;
const unsigned long delayTime2 = 1000;
const unsigned long baudRate = 9600;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
void setup(){
Serial.begin(baudRate);
pinMode(9, OUTPUT); // For LED blink
digitalWrite(9, LOW);
}
void loop(){
unsigned long currentMillis = millis(); // Get current time
if(myTimer1(delayTime, currentMillis) == 1) // First event
{
reading = analogRead(0); // Heart beat leading edge detected.
if(reading > UpperThreshold && IgnoreReading == false)
{
if(FirstPulseDetected == false)
{
FirstPulseTime = millis();
FirstPulseDetected = true;
}
else
{
SecondPulseTime = millis();
PulseInterval = SecondPulseTime - FirstPulseTime;
FirstPulseTime = SecondPulseTime;
}
IgnoreReading = true;
digitalWrite(9, HIGH);
}
if(reading < LowerThreshold && IgnoreReading == true) // Heart beat trailing edge detected.
{
IgnoreReading = false;
digitalWrite(9, LOW);
}
BPM = (1.0/PulseInterval) * 60.0 * 1000; // Calculate Beats Per Minute.
}
if(myTimer2(delayTime2, currentMillis) == 1) // Second event
{
Serial.print(reading);
Serial.print("\t");
Serial.print(PulseInterval);
Serial.print("\t");
Serial.print(BPM);
Serial.println(" BPM");
Serial.flush();
}
}
int myTimer1(long delayTime, long currentMillis) // First event timer
{
if(currentMillis - previousMillis >= delayTime) {previousMillis = currentMillis;return 1;}
else{return 0;}
}
int myTimer2(long delayTime2, long currentMillis) // Second event timer
{
if(currentMillis - previousMillis2 >= delayTime2) {previousMillis2 = currentMillis;return 1;}
else{return 0;}
}