-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheartBeats.py
More file actions
95 lines (72 loc) · 3.34 KB
/
heartBeats.py
File metadata and controls
95 lines (72 loc) · 3.34 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import time
from ISStreamer.Streamer import Streamer
import adafruit_ads1x15
if __name__ == '__main__':
streamer = Streamer(bucket_name="demo",bucket_key="MP5C4E8FD3VS",access_key="ist_VyBltj0nrsQHMzsAQVwFVSnqj4MIea_9")
adc = adafruit_ads1x15.ADS1115()
GAIN = 2/3
curState = 0
thresh = 525
P = 512
T = 512
stateChanged = 0
sampleCounter = 0
lastBeatTime = 0
firstBeat = True
secondBeat = False
Pulse = False
IBI = 600
rate = [0]*10
amp = 100
z = 0
lastTime = int(time.time()*1000)
while True:
Signal = adc.read_adc(0, gain=GAIN)
curTime = int(time.time()*1000)
sampleCounter += curTime - lastTime;
lastTime = curTime
N = sampleCounter - lastBeatTime;
if Signal < thresh and N > (IBI/5.0)*3.0 :
if Signal < T :
T = Signal;
if Signal > thresh and Signal > P:
P = Signal;
if N > 250 :
if (Signal > thresh) and (Pulse == False) and (N > (IBI/5.0)*3.0) :
Pulse = True; # set the Pulse flag when we think there is a pulse
IBI = sampleCounter - lastBeatTime;
lastBeatTime = sampleCounter;
if secondBeat : # if this is the second beat, if secondBeat == TRUE
secondBeat = False; # clear secondBeat flag
for i in range(0,10): # seed the running total to get a realisitic BPM at startup
rate[i] = IBI;
if firstBeat : # if it's the first time we found a beat, if firstBeat == TRUE
firstBeat = False; # clear firstBeat flag
secondBeat = True; # set the second beat flag
continue
runningTotal = 0;
for i in range(0,9):
rate[i] = rate[i+1];
runningTotal += rate[i];
rate[9] = IBI;
runningTotal += rate[9];
runningTotal /= 10;
BPM = 60000/runningTotal;
streamer.log("Heart-Beat Rate",BPM)
print ('BPM: {}'.format(BPM))
if Signal < thresh and Pulse == True :
Pulse = False;
amp = P - T;
thresh = amp/2 + T;
P = thresh;
T = thresh;
if N > 2500 :
thresh = 512;
P = 512;
T = 512;
lastBeatTime = sampleCounter;
firstBeat = True;
secondBeat = False;
print (z)
time.sleep(0.005)
streamer.close()