Zeheng Chen
/
funky
heart rate test
Diff: main.cpp
- Revision:
- 0:901c98b3591a
- Child:
- 1:b7f42e8e04fc
diff -r 000000000000 -r 901c98b3591a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 13 22:54:38 2015 +0000 @@ -0,0 +1,96 @@ +#include "mbed.h" +#include "math.h" +Ticker flipper; +Ticker flipper2; +DigitalOut led2(LED2); +AnalogIn sensor(p20); +AnalogIn sensor_visible(p19); +AnalogIn sensor_invisible(p18); + +//680 nm +#define visible 68 +//940 nm +#define invisible 94 + + + +float data =0; +float previous_data =0; +int count1=-1; +int count2=-1; +float max1 =0.0; +float max2 =0.0; +int running_count=0; +bool foundMax = false; +#define difference 0.05 +#define count_period 0.1 +void flip() { + led2 = !led2; + previous_data = data; + // printf("Data = %f\t",data); + data = sensor.read() * 3.3; + running_count++; + // printf("data = %f\n",data); + if (data - previous_data > 0){ + //increasing + printf("Increasing \n"); + //do nothing + } + else if(data - previous_data < 0 && !foundMax){ + //check if foundMax + printf("found max \n"); + foundMax = true; + count1 = running_count; + max1 = data; + } + else{ + printf("Decrease\n"); + } + if(max1-data < difference && data-previous_data >0 && foundMax){ + //found second max; + printf("found second max \n"); + count2 = running_count; + float period = (count2 - count1)* count_period; + + float BPM = (1/period) * 60; + printf("BPM = %f",BPM); + exit(1); + //reset + count1 = 0; + count2 =0; + foundMax = false; + max1 =0; + running_count =0; + } + +} + +void findOxygenSaturation(){ + + float v = log10f(sensor_visible.read()*3.3) * visible; + float iv = log10f(sensor_invisible.read()*3.3) * invisible; + printf("visible = %f\n",v); + printf("invisible = %f\n",iv); + float ratio = v/iv; + printf("ratio = %f\n",ratio); + float spo = -0.3393 * ratio + 1.1595 ; + printf("spo = %f\n",spo); +} + + +int main() { + led2 = 1; + //comment out the below line to test heart rate + //the heart rate test subroutine, once it find the heart rate, it will terminate the program. + //so make sure you reset the MBED + //flipper.attach(&flip, count_period); // the address of the function to be attached (flip) and the interval (1 seconds) + + + //read OxygenSaturation Every two second. + //use pin 19 for visible light input + //use pin 18 for invisible light input + flipper2.attach(&findOxygenSaturation,2.0); + while(1) { + + } +} \ No newline at end of file