David Ehrlich / breath_test

Files at this revision

API Documentation at this revision

Comitter:
dehrlich
Date:
Wed Nov 02 02:11:24 2016 +0000
Commit message:
test for a 1 second rescue breath using modern device windsensor rev. c

Changed in this revision

breath_test.cpp Show annotated file Show diff for this revision Revisions of this file
breath_test.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/breath_test.cpp	Wed Nov 02 02:11:24 2016 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include <vector> //added for Lab1 part 3
+#include <algorithm> //added for Lab1 part 3
+#include <assert.h>     /* assert */
+AnalogIn windSensor(p17);
+DigitalOut myled(LED1);
+Timer t, q;
+vector<float> b;
+vector<float> c;
+template<typename T>
+
+void pop_front(std::vector<T>& vec)
+{
+    assert(!vec.empty());
+    vec.erase(vec.begin());
+}
+
+float give_breath(void) {
+    float a = 0.0;
+    float d = 0.0; //return time for fractional breathes
+    float e = 0.0;
+    int i;
+    float thresh;
+    for(i = 0; i<20; i++){
+        a = a + windSensor;
+    }
+    a = a/20.0; //get first baseline reading
+    thresh = a*3.0; //theshold to start breathe timer (ideal multiplier is between 2-3)
+    t.start();
+    while(1){
+        if(t.read() <= 3.0){
+          wait(0.1); //necessary to give sensor time to be polled
+          if(windSensor > thresh){
+            q.start();
+            myled = 1;
+            for(i = 0; i<10; i++){
+                c.push_back(windSensor);
+            }
+            for(i=0; i<10; i++){
+                e = e + c[i];
+            }
+            e = e/10.0; //provides slope of 10 most recent samples
+            while( (e > thresh) && (q.read() < 1.0)){
+
+              pop_front(c);//remove the oldest element
+              c.push_back(windSensor);
+              for(i=0; i<10; i++){
+                e = e + c[i];
+              }
+              e = e/10.0; //provides slope of 10 most recent samples
+              
+              }
+              d = q.read();
+          if(q.read() > 1.0){ //breathe was greater than thresh and lasted more than 1 sec
+            return 1.0; //full breath
+          }
+          else{
+              return d; //partial breath
+          }
+        }
+      }
+      else{
+        return 0.0; //no breath in 3 seconds
+        }
+    } 
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/breath_test.h	Wed Nov 02 02:11:24 2016 +0000
@@ -0,0 +1,3 @@
+//KickStart My Heart victim breath test algorithm 
+
+float give_breath(void);
\ No newline at end of file