heart rate test

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
zchen78
Date:
Tue Apr 14 05:16:32 2015 +0000
Parent:
5:6db460f13fcc
Commit message:
tons of hack around

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6db460f13fcc -r 95e2e305e802 main.cpp
--- a/main.cpp	Tue Apr 14 04:04:24 2015 +0000
+++ b/main.cpp	Tue Apr 14 05:16:32 2015 +0000
@@ -43,11 +43,11 @@
         if(firstRun){
             float diff = data - global_max;
             float negative = -1*difference;
-            printf("HERE\n");
-            printf("diff = %f\n",diff);
-            printf("data = %f\n",data);
+           // printf("diff = %f\n",diff);
+         //   printf("difference = %f,\t \t \t negative = %f \n", difference, negative);
+            //printf("data = %f\n",data);
             printf("global_max = %f\n",global_max);
-            printf("HERE End\n");
+            //printf("------------ End\n");
             if(diff < difference && diff >negative) {
                 printf("Within the range of first max, confident\n");
                 printf("found max, data= %f\n",data);        
@@ -65,40 +65,49 @@
         } 
     }
     else if(data - previous_data < 0 && foundMax){
-        if(((max1-data) < difference && (max1-data) > -1*difference)){
+        printf("Decrease \t \n");
+        if(((max1-data) < difference) && ((max1-data) > -1*difference)){
         //found second max; 
             printf("max1 = %f\n",max1);
             printf("found second max,data = %f \n",data);
             count2 = running_count;     
             float period = (count2 - count1)* count_period; 
-            float BPM = (1/period) * 60;
+            float BPM = (1.0/period) * 60.0;
             printf("Period = %f\n",period);
             printf("BPM = %f\n",BPM);
-            exit(1);    
+          //  exit(1);
+            wait(5);
+            reset();     
             printf("\n");
             printf("\n");
             printf("\n");
+        }
+        else {
+            if((running_count - count1)*count_period > 1.5) {
+                printf("Optimization filter by hand \t, reset\n");
+                reset();    
+            }    
         }   
     }
-    
     else{
     }
-    
-    if(!foundMax && running_count>250){
+    //reset if not found max for 5 seconds. 
+    if(!foundMax && running_count*count_period>5){
         reset();
-        global_max = normalization();    
-        printf("new normalization = %f",global_max);    
     }
 }
 
 void reset(){
         //reset
         printf("reset\n");
+        global_max = normalization(); 
+        printf("new normalization = %f",global_max);       
         count1 = 0;
         count2 = 0; 
         foundMax = false; 
-        max1 = 0;
+        max1 = 0.0;
         running_count = 0; 
+        firstRun = true; 
 }
 
 float normalization(){
@@ -106,12 +115,12 @@
     float data_input;
     //normalize the data input 
     //find the max in 5 seconds; 
-    for (int x=0;x<100;x++){
+    for (int x=0;x<50;x++){
         data_input = sensor.read() * 3.3;
         if(data_input > max_input)max_input = data_input; 
         wait_ms(50);
     }
-    difference = max_input * 0.2; 
+    difference = max_input * 0.35; 
     return max_input; 
 }