A code to drive a 3sensor reading unit for monitoring the operation opf a closed circuit rebreather (CCR) with 3 electrogalvanic sensors. Also uses a DS1307 for realtime clock and an MPX5700 to read the depth (mounted inside the breathing loop to keep it 'dry'). circuit diagrams available on rebreather world.

Dependencies:   DS1307 TextOLED mbed

Revision:
4:74df6d31ee0a
Parent:
3:0d94a277aa8c
Child:
5:35417986539a
--- a/Rebmon_main.cpp	Fri Aug 03 11:29:30 2012 +0000
+++ b/Rebmon_main.cpp	Fri Aug 03 14:19:44 2012 +0000
@@ -45,6 +45,7 @@
 int date = 0;
 int month = 0;
 int year = 0;
+int seconds=0; // general number of seconds since 2000 etc timestamp variable
 
 int scrubtime=0; // these are expressed in minutes
 int divetime=0;
@@ -64,10 +65,10 @@
 int getseconds() {
     my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year);
     //simple timestamp = # seconds since midnight jan 1st 2000 if all months were 30 days.
-    int seconds=year*365*24*60*60+month*30*24*60*60+day*24*60*60+hours*60*60+min*60+sec;
+    int secondst=year*365*24*60*60+month*30*24*60*60+day*24*60*60+hours*60*60+min*60+sec;
     //simple timestamp = # seconds since midnight jan 1st 2000 if all months were 30 days....
     // ie wrong but simpler than the real thing
-    return(seconds);
+    return(secondst);
 }
 
 
@@ -100,7 +101,7 @@
     g_lcd.cls();
     for (count=20; count>0; count--) {
         g_lcd.locate(0,0);
-        g_lcd.printf("Calibrating %.2d",count);
+        g_lcd.printf("Calibrate 21%% %.2d",count);
         ppo1=ppo1+EG1;
         ppo2=ppo2+EG2;
         pres=pres+PRESin;
@@ -122,6 +123,13 @@
     fclose(fp); //NB file system locked on write so must make sure we close files in case want to reprogram etc...
 }
 
+// sub to test if a variable is an even number
+int iseven(int g) {
+    int test=0;
+    if(g%2 ==0) test=1;
+    return(test);
+}
+
 
 void status() {
     if (state==0) {
@@ -129,9 +137,10 @@
         g_lcd.character(8,0,6); // surface icon
     }
     if (state==1) g_lcd.character(8,0,6); // surface icon
-    if (state==2 && flash==1) g_lcd.character(8,0,4); // diving icon
-    if (state==2 && flash==0) g_lcd.character(8,0,68); // diving icon
-    }
+    if (state==2 && iseven(seconds)==1) g_lcd.character(8,0,4); // diving icon
+    if (state==2 && iseven(seconds)==0) g_lcd.character(8,0,68); // diving icon
+
+}
 
 // warning and LED conditions
 
@@ -140,9 +149,42 @@
 
 }
 
+// pick maximum of two values
+float maximum(float a,float b) {
+    float maximum;
+    if (a>b) maximum=a;
+    else maximum=b;
+    return(maximum);
+}
+
+// pick minimum  of two values
+float minimum(float a,float b) {
+    float minim;
+    if (a<b) minim=a;
+    else minim=b;
+    return(minim);
+}
+
+
+
 void leds() {
+// first turn everything off
+red=0;
+green=0;
+blue=0;
+float ppo;
+ppo=maximum(ppo1,ppo2); // use max value to compute leds...
+if(ppo<0.2 && flash==1) red=1; // flashing red means very bad things - getting low on oxygen!!!
+if(ppo>0.2 && ppo < 1) red=1; // non-flashing red
+if(ppo>=1.0 && ppo <1.2) {red=1;green=1;} // red-green
+if(ppo<1.3 && ppo >=1.2) green=1; // green - optimal range in ccr mode
+if(ppo<1.4 && ppo >=1.3){green=1;blue=1;} // green-blue - high ppo2 be careful of spiking
+if(ppo2<1.6 && ppo2>=1.4) blue=1; // DANGE ble high ppo2
+if(ppo2>=1.6 && flash==1) blue=1;
 }
 
+
+
 //read battery state and insert the battery symbol
 void battery() {
     int batsym=0;
@@ -177,21 +219,6 @@
 }
 
 
-// pick maximum of two values
-float maximum(float a,float b) {
-    float maximum;
-    if (a>b) maximum=a;
-    else maximum=b;
-    return(maximum);
-}
-
-// pick minimum  of two values
-float minimum(float a,float b) {
-    float minim;
-    if (a<b) minim=a;
-    else minim=b;
-    return(minim);
-}
 
 
 
@@ -216,19 +243,14 @@
     //DEBUG
 
 }
-// sub to test if a variable is an even number
-/*int iseven(int g) {
-    int test=0;
-    if ((float)g/2==(int)((float)g/2)) test=1;
-    return(test);
-}*/
+
 
 int main() {
 // first some local variables
     int startuptime=getseconds();
-    int startdive=0,endclock=0;; // value of seconds when dive starts and counter to decide if dive complete...
+    int startdive=0,endclock=0; // value of seconds when dive starts and counter to decide if dive complete...
 
-    int seconds=0,minutes=0,dt=0;; // minutes is elapsed minutes since start of prog
+    int minutes=0,dt=0;; // minutes is elapsed minutes since start of prog
     int i=0,j=0; // general loop counting variables
 
 
@@ -239,13 +261,13 @@
     g_lcd.locate(0,1);
     g_lcd.printf("CAL?");
     battery();
-j=0;
+    j=0;
 // hang about waiting for the cal switch to be pressed in ccase it is
     while (seconds-startuptime<20) {
         seconds=getseconds();
         g_lcd.locate(5,1);
         g_lcd.printf("%.2d",21-(seconds-startuptime));
-        if(j>1) flash=1;
+        if (j>1) flash=1;
         else flash=0;
         battery(); // bung in battery symbol.
         g_lcd.locate(7,0);
@@ -267,7 +289,7 @@
         wait(0.2); //stop screen flicker
         readsensors();
         seconds=getseconds();
-        minutes=(int)((float)seconds-(float)startuptime)/60;
+        minutes=(int)(((float)seconds-(float)startuptime)/60);
         dt=seconds-startuptime; // elapsed seconds
 
         if (j>1) flash=1;
@@ -277,19 +299,21 @@
 
         // setup state variable
         if (minutes<1) state=0; // startup mode - do nothing just wait to allow sensor readings to settle.
-        if (minutes>=1) state=1; // surface mode - ok to go for a dive now
-        if (minutes>1 && depth>0.5 && state==1) {
+        if (minutes>=1 && state==0) state=1; // surface mode - ok to go for a dive now
+        if (minutes>=1 && depth>0.5 && state==1) {
             state=2; // enter dive mode
-            startdive=seconds; // set start of divetime.
+            if (startdive==0) startdive=seconds; // set start of divetime. don't do this twice
             endclock=0; // reset end of dive clock
         }
         if (state==2) {
-            divetime=(int)((float)seconds-(float)startdive)/60; // time since start of dive in minutes.
+            divetime=(int)(((float)seconds-(float)startdive)/60); // time since start of dive in minutes.
             // do deco calcs here when implemented
             if (depth<0.3) {
-                endclock++;
-                if (endclock==150) state=1; // 30s at shallower than 0.3m and we return to surface mode.
+                endclock=endclock+1;
+
+                if (endclock>150) state=1; // 30s at shallower than 0.3m and we return to surface mode.
             }
+            scrubtime=minutes; // need to add memory of prior scrub time to this once variable input fom log file is implements
         }