-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsta-pulse.ino
More file actions
74 lines (53 loc) · 1.91 KB
/
Copy pathinsta-pulse.ino
File metadata and controls
74 lines (53 loc) · 1.91 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
/**
* insta-pulse.ino
* a basic "hello world" sketch that demonstrates reading the 5v TTL pulse,
* triggering an LED & calculating beats-per-minute (BPM) using the inter-beat interval (IBI)
* */
int inPin = 2; // insta-pulse input pin
int outPin = 13; // LED
int bpm = 0; // beats per minute
float ibi = 0.0; // inter-beat interval
unsigned long time = 0; // inter-beat interval counter
int sensorVal = LOW; // sensor value, HIGH or LOW
void setup() {
//start serial connection, go fast
Serial.begin(115200);
pinMode(inPin, INPUT); // insta-pulse input pin
pinMode(outPin, OUTPUT); // LED pin
// BEGIN TIMER SETUP
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;// initialize counter value to 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (16*10^6) / (1000*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();// allow interrupts
// END TIMER SETUP
}
ISR(TIMER1_COMPA_vect) { // Interrupt at freq of 1kHz to measure insta-pulse
cli(); // stop interrupts
time++; // increment by 1kz = .001 second
if(time > 250){ // debounce: 250 = a heart rate of about 240 bpm
sensorVal = digitalRead(inPin); // read the insta-pulse
if (sensorVal) { // if pulse detected
ibi = float(time)/1000.0; // calculate inter-beat interval in seconds
bpm = int(60.0/ibi); // calculate beats per minute
Serial.println(bpm); // print out bpm
digitalWrite(outPin,HIGH); // blink the LED
time = 0; // reset the ibi counter
} else {
digitalWrite(outPin,LOW); // kill the LED
}
}
sei(); // allow interrupts
}
void loop() {
// do other stuff here, control lights, sound, whatever...
}