-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (86 loc) · 2.48 KB
/
app.js
File metadata and controls
105 lines (86 loc) · 2.48 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
96
97
98
99
100
101
102
103
104
105
var rate = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
sampleCounter = 0,
lastBeatTime = 0,
P = 512,
T = 512,
thresh = 525,
amp = 100,
firstBeat = true,
secondBeat = false,
IBI = 600,
Pulse = false,
BPM,
Signal,
QS = false;
var five = require('johnny-five'),
board, sensor;
board = new five.Board();
board.on('ready', function() {
sensor = new five.Sensor({
pin: "A0",
freq: 2
});
sensor.scale([0,1024]).on('read', function() {
Signal = this.scaled;
calculate_bpm();
if(QS === true) {
console.log('BPM : ', BPM);
QS = false;
}
});
});
function calculate_bpm() {
sampleCounter += 2;
N = sampleCounter - lastBeatTime;
if(Signal < thresh && N > (IBI/5)*3) {
if (Signal < T) {
T = Signal;
}
}
if(Signal > thresh && Signal > P) {
P = Signal;
}
if (N > 250) {
if ((Signal > thresh) && (Pulse === false) && (N > (IBI/5)*3)) {
Pulse = true;
IBI = sampleCounter - lastBeatTime;
lastBeatTime = sampleCounter;
if(secondBeat) {
secondBeat = false;
for(var i=0; i<=9; i++){
rate[i] = IBI;
}
}
if(firstBeat) {
firstBeat = false;
secondBeat = true;
return;
}
var runningTotal = 0;
for(var i=0; i<=8; i++) {
rate[i] = rate[i+1];
runningTotal += rate[i];
}
rate[9] = IBI;
runningTotal += rate[9];
runningTotal /= 10;
BPM = 60000/runningTotal;
QS = true;
}
}
if (Signal < thresh && 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;
}
}