heart rate test

Dependencies:   mbed

Revision:
2:d7d21a7491aa
Parent:
1:b7f42e8e04fc
Child:
3:fe75390a6f30
--- a/main.cpp	Tue Apr 14 01:18:52 2015 +0000
+++ b/main.cpp	Tue Apr 14 03:01:29 2015 +0000
@@ -12,8 +12,6 @@
 //940 nm
 #define invisible 94
 
- 
-
 float data =0; 
 float previous_data =0;
 int count1=-1;
@@ -24,8 +22,10 @@
 int running_count=0;  
 bool foundMax = false;
 bool firstRun = true; 
-#define difference 0.05 
+#define difference 0.05
 #define count_period 0.1
+void reset();
+float normalization();
 void flip() {
     led2 = !led2;
     previous_data = data; 
@@ -33,6 +33,7 @@
     data = sensor.read() * 3.3;
     running_count++;  
    // printf("data = %f\n",data);
+    
     if (data - previous_data > 0){
         //increasing 
         printf("Increasing \n");
@@ -40,9 +41,10 @@
     }     
     else if(data - previous_data < 0 && !foundMax){
         //check if foundMax
+        printf("Decrease\n");    
         if(firstRun){
             float diff = data - global_max;
-            if(diff < difference && diff >0) {
+            if(diff < difference || diff >-1*difference) {
                 printf("Within the range of first max, confident\n");
                 printf("found max, data= %f\n",data);        
                 foundMax = true;
@@ -52,7 +54,6 @@
             }
         }
         else{
-                printf("Future Case, no need of global_max\n");
                 printf("found max, data= %f\n",data);        
                 foundMax = true;
                 count1 = running_count; 
@@ -62,26 +63,48 @@
     else{
         printf("Decrease\n");    
     }
+    
+    if(!foundMax && running_count>250){
+        reset();
+        global_max = normalization();    
+        printf("new normalization = %f",global_max);    
+    }
     if(max1-data < difference && data-previous_data >0 && foundMax){
         //found second max; 
         printf("found second max,data = %f \n",data);
         count2 = running_count;     
         float period = (count2 - count1)* count_period; 
         float BPM = (1/period) * 60;
-        printf("BPM = %f",BPM);
+        printf("Period = %f\n",period);
+        printf("BPM = %f\n",BPM);
         exit(1);
         //reset 
+      //  count1 = 0;
+        //count2 = 0; 
+        //foundMax = false; 
+        //max1 = 0;
+        //running_count = 0; 
+       // reset();
+      //  wait(5);
+        printf("\n");
+        printf("\n");
+        printf("\n");
+    }
+}
+
+void reset(){
+        //reset
+        printf("reset\n");
         count1 = 0;
         count2 = 0; 
         foundMax = false; 
         max1 = 0;
         running_count = 0; 
-    }
 }
 
-int normalization(){
-    int max_input = -1; 
-    int data_input;
+float normalization(){
+    float max_input = -1.0; 
+    float data_input;
     //normalize the data input 
     //find the max in 5 seconds; 
     for (int x=0;x<100;x++){