Optical heart rate monitor using photoresistor and LED to calculate BPM, which is outputted to LCD screen.
Dependencies: 4DGL-uLCD-SE mbed
Fork of uLCD144G2_demo by
Revision 10:ad2548407023, committed 2015-10-28
- Comitter:
- maryannionascu
- Date:
- Wed Oct 28 17:55:39 2015 +0000
- Parent:
- 9:b2a894f94cb7
- Commit message:
- Updated comments
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r b2a894f94cb7 -r ad2548407023 main.cpp --- a/main.cpp Thu Oct 22 20:24:19 2015 +0000 +++ b/main.cpp Wed Oct 28 17:55:39 2015 +0000 @@ -17,7 +17,8 @@ volatile unsigned long lastBeatTime = 0; //time of last beat volatile float peak = 25; //peak of heartbeat wave; initilized to 20 to avoid noise at startup volatile float trough = 15; //trough of heartbeat wave -volatile float thresh = 10; // used to reduce noice +volatile float lowerThresh = 10; // used to reduce noice; change these values based on amount of light in room +volatile float upperThresh = 30; // used to reduce noice; change these values based on amount of light in room volatile float amp = 5; // amplitude of heartbeat wave volatile bool firstBeat = true; //keeps track of first beat found volatile bool beatFound = false; //true when a beat is found, false in between beats @@ -28,19 +29,19 @@ beat = photoresistor * 100; // read the analog data from the photoresistor and multiply by 100 (amplifies small values) currIBI = t.read() - lastBeatTime; // avoid noise by waiting 3/5 of last IBI - if((beat < thresh) && (currIBI > (lastIBI*3)/5)){ + if((beat < lowerThresh) && (currIBI > (lastIBI*3)/5)){ if (beat < trough){ trough = beat; } } - if((beat > thresh) && (beat > peak)){ + if((beat > upperThresh) && (beat > peak)){ peak = beat; } // photoresistor signal surges when there is a heartbeat - if ((currIBI > 0.25) && (beat > thresh) && (beat > 30) && (beatFound == false)){ + if ((currIBI > 0.25) && (beat > upperThresh) && (beatFound == false)){ beatFound = true; beatCount++; - thresh = beat - 0.7; //adjust threshold to account for any change in lighting and avoid noise + lowerThresh = beat - 0.7; //adjust threshold to account for any change in lighting and avoid noise lastIBI = t.read() - lastBeatTime; lastBeatTime = t.read(); //if this is the first beat found, fill pastIBI values with this value until more beats are found @@ -65,23 +66,13 @@ } } //when the signal is less than the threshold, the beat is over - if (((beat < thresh) && beatFound) || (currIBI > 2)){ + if (((beat < lowerThresh) && beatFound) || (currIBI > 2)){ beatFound = false; // reset the beatFound flag to find the next beat amp = peak - trough; // calculate amplitude of heartbeat wave - thresh = amp/2 + trough; // set threshold to 50% of the amplitude - peak = thresh; - trough = thresh; + upperThresh = amp/2 + trough; // set upper threshold to 50% of the amplitude + peak = upperThresh; + trough = lowerThresh; } - /* - //if 3 seconds pass since the last beat, reset all variables to default values - if (currIBI > 3){ - thresh = 10; - peak = 10; - trough = 10; - lastBeatTime = t.read(); - firstBeat = true; - } - */ __enable_irq(); //enable interrupts } @@ -119,23 +110,8 @@ else{ uLCD.locate(1,2); uLCD.printf("\nCalculating...\n"); - //uLCD.printf("%2F\n", (float) photoresistor*100); - //uLCD.printf("beatCount: %2F\n", (float) beatCount); - //uLCD.printf("currIBI: %2F\n", (float) currIBI); wait(.1); uLCD.cls(); - /* - uLCD.printf("%2F\n", (float) photoresistor*100); - //uLCD.printf("BPMfound: %2F\n", (float) BPMfound); - uLCD.printf("beatFound: %2F\n", (float) beatFound); - uLCD.printf("beatCount: %2F\n", (float) beatCount); - //uLCD.printf("firstBeat: %2F\n", (float) firstBeat); - uLCD.printf("thresh: %2F\n", (float) thresh); - //uLCD.printf("sumIBI: %2F\n", (float) sumIBI); - //uLCD.printf("time: %2F\n", (float) t.read()); - wait(.1); - uLCD.cls(); - */ } } }