Software to drive a monitor unit for a closed circuit rebreather using 3 electrogalvanic oxygen sensor cells run through an amplifier (lm324) . Uses a separate ds1307 clock IC to get timestamp values for logged data.

Dependencies:   DS1307 TextOLED_custom mbed

The main electornics is housed in another pod mounted on the back of the unit. I'm using an mbed lpc11u24 to drive everything which comes with a flash drive for data logging built in. It has an external ds1307 clock chip added and a very cheapo lm324 quad op-amp to amplify the o2 sensor signals from the 10s of mV range by 30x so that ppo2=0.21 corresponds to about 0.3V. I still have to do some ADC averaging with this amplifier and do have to calibrate out the individual offsets of the chip but this works ok now that I've worked out which amp is on which adc...

Files at this revision

API Documentation at this revision

Comitter:
pegcjs
Date:
Fri Aug 03 14:19:44 2012 +0000
Parent:
3:0d94a277aa8c
Child:
5:35417986539a
Commit message:
leds done bu need to sort out logging data and the recall of saved settings and scrub time in case of swirtchoff between dives on same scrubber.

Changed in this revision

Rebmon_main.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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
         }