attempt to fix posible power issues with the sharp

Dependencies:   ADS1115 BME280 CronoDot SDFileSystem mbed

Fork of Outdoor_UPAS_v1_2_Tboard by scott kelleher

Revision:
12:5b4f3245606a
Parent:
11:aa21628a9b15
Child:
13:455601f62aad
--- a/main.cpp	Thu Mar 03 16:06:05 2016 +0000
+++ b/main.cpp	Mon Mar 21 23:14:46 2016 +0000
@@ -1,4 +1,6 @@
 #include "mbed.h"
+
+/*
 #include "SDFileSystem.h"
 #include "Adafruit_ADS1015.h"
 #include "MCP40D17.h"
@@ -9,34 +11,99 @@
 #include "NCP5623BMUTBG.h"
 #include "CronoDot.h"
 #include "EEPROM.h"
-#include "Calibration.h" 
-Serial pc(USBTX, USBRX);
-Serial microChannel(PB_10, PB_11); // tx, rx
-//Timer t;
-struct tm tt;
+#include "Calibration.h"
+#include "MAX_M8.h" 
+#include "DRV8830.h"
+*/
 
-I2C                 i2c(D14, D15);
-Adafruit_ADS1115    ads(&i2c);
+/////////////////////////////////////////////
+//General Items
+/////////////////////////////////////////////
+//I2C                 i2c(PB_9, PB_8);//(D14, D15);
+Serial              pc(USBTX, USBRX);
+DigitalOut          blower(PA_9, 0);//(D8, 0);
+DigitalOut          pbKill(PC_12, 1); // Digital input pin that conncect to the LTC2950 battery charger used to shutdown the UPAS 
+/*
+DigitalIn           nINT(PA_15); //Connected but currently unused is a digital ouput pin from LTC2950 battery charger. http://cds.linear.com/docs/en/datasheet/295012fd.pdf
 MCP40D17            DigPot(&i2c);
-BME280              bmesensor(D14, D15);
-STC3100             gasG(D14, D15);
-DigitalOut          blower(D8, 0);
-DigitalOut          pbKill(PC_12, 1);
+BME280              bmesensor(PB_9, PB_8);//(D14, D15);
+NCP5623BMUTBG       RGB_LED(PB_9, PB_8);//(D14, D15);
+CronoDot            RTC_UPAS(PB_9, PB_8);//(D14, D15);
+EEPROM              E2PROM(PB_9, PB_8);//(D14, D15);
+Calibration         calibrations(1);     //Default serial/calibration if there are no values for the selected option
+*/
+
+/////////////////////////////////////////////
+//RN4677 BT/BLE Module
+/////////////////////////////////////////////
+Serial microChannel(PB_10, PB_11); // tx, rx
 DigitalOut          bleRTS(PB_14, 0);
 DigitalOut          bleCTS(PB_13, 0);
-LSM303              movementsensor(D14, D15);
-SI1145              lightsensor(D14, D15);
-NCP5623BMUTBG       RGB_LED(D14, D15);
-CronoDot            RTC_UPAS(D14, D15);
-EEPROM              E2PROM(D14, D15);
-//DigitalOut          GPS_EN(p4,0);       //pin 4 is used to enable and disable the GPS, in order to recive serial communications
-Calibration         calibrations(1);     //Default serial/calibration if there are no values for the selected option
+DigitalOut          BT_IRST(PC_8, 0);
+DigitalOut          BT_SW(PA_12, 0);
+
+/*
+/////////////////////////////////////////////
+//Analog to Digital Converter
+/////////////////////////////////////////////
+Adafruit_ADS1115    ads(&i2c);
+//DigitalIn           ADS_ALRT(PA_10); //Connected but currently unused. (ADS1115) http://www.ti.com/lit/ds/symlink/ads1115.pdf
+
+/////////////////////////////////////////////
+//Battery Monitoring
+/////////////////////////////////////////////
+STC3100             gasG(PB_9, PB_8);//(D14, D15);  // http://www.st.com/web/en/resource/technical/document/datasheet/CD00219947.pdf
+DigitalIn           bcs1(PC_9); //Charge complete if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
+DigitalIn           bcs2(PA_8); //Batt charging if High. Connected but currently unused. (MCP73871) http://www.mouser.com/ds/2/268/22090a-52174.pdf
+
+/////////////////////////////////////////////
+//Accelerometer and Magnometer
+/////////////////////////////////////////////
+LSM303              movementsensor(PB_9, PB_8);//(D14, D15); // http://www.st.com/web/en/resource/technical/document/datasheet/DM00027543.pdf
+//DigitalIn           ACC_INT1(PC_7); //Connected but currently unused. (LSM303)
+//DigitalIn           ACC_INT2(PC_6); //Connected but currently unused. (LSM303)
+//DigitalIn           ACC_DRDY(PC_11); //Connected but currently unused. (LSM303)
 
+/////////////////////////////////////////////
+//UV and Visible Light Sensor
+/////////////////////////////////////////////
+SI1145              lightsensor(PB_9, PB_8);//(D14, D15);
+//DigitalIn           UV_INT(PD_2); //Connected but currently unused nor configured (interupt). (SI1145) https://www.silabs.com/Support%20Documents/TechnicalDocs/Si1145-46-47.pdf
 
+/////////////////////////////////////////////
+//GPS
+/////////////////////////////////////////////
+Max_M8 gps(PB_9, PB_8,(66<<1));   // this must be defnined in the int main (? Not sure if this is true)
+
+/////////////////////////////////////////////
+//Hbridge Valve Control
+/////////////////////////////////////////////
+DRV8830    motor1(PB_9, PB_8, 0xC4); 
+DRV8830    motor2(PB_9, PB_8, 0xCA); 
+DRV8830    motor3(PB_9, PB_8, 0xCC); 
+DRV8830    motor4(PB_9, PB_8, 0xCE); 
+DigitalIn  hb_fault1(PA_6);
+DigitalIn  hb_fault2(PA_7);
+DigitalIn  hb_fault3(PA_5);
+DigitalIn  hb_fault4(PA_4);
+
+/////////////////////////////////////////////
+//SD Card
+/////////////////////////////////////////////
+char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
+SDFileSystem sd(PB_5, PB_4, PB_3, PB_6, "sd");//(D4, D5, D3, D10, "sd"); // (MOSI, MISO, SCK, SEL)
+DigitalIn    sdCD(PA_11);
+*/
+/////////////////////////////////////////////
+//Callbacks
+/////////////////////////////////////////////
 Ticker          stop;     //This is the stop callback object
 Ticker          logg;     //This is the logging callback object
 Ticker          flowCtl;  //This is the control loop callback object
 
+/////////////////////////////////////////////
+//Varible Definitions
+/////////////////////////////////////////////
 uint16_t serial_num = 1;                // Default serial/calibration number
 int RunReady =0;
 
@@ -88,18 +155,32 @@
 int digitalpotMax = 127;
 int digitalpotMin = 10;
 
-int dutyUp = 4;
-int dutyDown = 3;
+int dutyUp;
+int dutyDown;
+
+float flat = 0;
+float flon = 0;
 
-// variables are only place holders for the US_Menu //
-int refreshtime;
-//int refresh_Time = 10;   // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
-float home_lat, home_lon, work_lat, work_lon;
+uint8_t gpssatellites = 0;
+double gpsspeed = 0.0;
+double gpslatitude = 0.0;
+double gpslongitude = 0.0;
+float gpsaltitude = 0.0;
+char gpslat = 'W';
+char gpslon = 'N';
+
+
+float home_lat  = 40.580508;
+float home_lon  = -105.081823;
+float work_lat  = 40.594062; //40.569136;
+float work_lon  = -105.075683; //-105.081966;
+int location = 0;
+
+float homeDistance = 99999;
+float workDistance = 99999;
+
 //*************************************************//
 
-char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
-SDFileSystem sd(D4, D5, D3, D10, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (MOSI, MISO, SCK, SEL)
-
 void sendData(); 
 
 
@@ -147,7 +228,7 @@
                 if((transmissionValue == 1 || transmissionValue == 2 || transmissionValue == 3 || transmissionValue == 4 || transmissionValue == 5 ||
                     transmissionValue == 6 || transmissionValue == 7) &&  rx_buf[rx_len-1] != 0xff)
                 {}else{
-                    if(transmissionValue == 4 ) sendData();
+                    //if(transmissionValue == 4 ) sendData();
                     if(transmissionValue == 8){
                          runReady = 1;
                          microChannel.attach(NULL,microChannel.RxIrq);
@@ -159,6 +240,7 @@
                 }
             }
         }
+       
         if(haltBLE!=1){
             
             if((transmissionValue!=100) && (dataLength!= 0)) writeData[dataLength-1] = rx_buf[0];
@@ -170,8 +252,10 @@
                 
                 //if(dataLength==6)RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);//sets chronodot RTC
                 if(dataLength==6){
-                        RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);//sets chronodot RTC
+                        //RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);//sets chronodot RTC
+                        ///////////////////////
                         //sets ST RTC
+                        //////////////////////
                         STtime.tm_sec = writeData[0];    // 0-59
                         STtime.tm_min = writeData[1];    // 0-59
                         STtime.tm_hour = writeData[2];   // 0-23
@@ -179,10 +263,11 @@
                         STtime.tm_mon = writeData[4]-1;     // 0-11
                         STtime.tm_year = 100+writeData[5];  // year since 1900 (116 = 2016)
                         time_t STseconds = mktime(&STtime);
-                        set_time(STseconds); // Set RTC time to 16 December 2013 10:05:23 UTC
+                        set_time(STseconds); // Set RTC time 
                     }
     
-            }else if(transmissionValue ==2){ //process and store sample start/end 
+            }
+            /*else if(transmissionValue ==2){ //process and store sample start/end 
                 if(dataLength ==12)E2PROM.write(0x00015, writeData, 12);
                 
             }else if(transmissionValue ==3){ //process and store sample name
@@ -197,6 +282,7 @@
             }else if(transmissionValue ==7){ //process and store Serial Number
                 if(dataLength ==2)E2PROM.write(0x00034,writeData,2);
             }
+            */
             dataLength++;        
         }
 
@@ -206,7 +292,9 @@
          uint8_t extract = microChannel.getc();
     }  
     
+    
 }
+/*
 void sendData(){
     
     uint8_t sampleTimePassValues[13] = {0x01,};
@@ -240,6 +328,59 @@
 
     
 } 
+*/
+//////////////////////////////////////////////////////////////
+// GPS: Degree-minute format to decimal-degrees
+//////////////////////////////////////////////////////////////
+double convertDegMinToDecDeg (float degMin)
+{
+    double min = 0.0;
+    double decDeg = 0.0;
+
+    //get the minutes, fmod() requires double
+    min = fmod((double)degMin, 100.0);
+
+    //rebuild coordinates in decimal degrees
+    degMin = (int) ( degMin / 100 );
+    decDeg = degMin + ( min / 60 );
+
+    return decDeg;
+}
+
+//////////////////////////////////////////////////////////////
+// GPS: Calculate distance from target location
+//////////////////////////////////////////////////////////////
+double GPSdistanceCalc (float tlat, float tlon)
+{
+    
+
+
+float tlatrad, flatrad;
+float sdlong,  cdlong;
+float sflat, cflat;
+float stlat, ctlat;
+float delta, denom;
+
+    double distance;
+    delta = (flon-tlon)*0.0174532925;
+    sdlong = sin(delta);
+    cdlong = cos(delta);
+    flatrad = (flat)*0.0174532925;
+    tlatrad = (tlat)*0.0174532925;
+    sflat = sin(flatrad);
+    cflat = cos(flatrad);
+    stlat = sin(tlatrad);
+    ctlat = cos(tlatrad);
+    delta = (cflat * stlat) - (sflat * ctlat * cdlong);
+    delta = pow(delta,2);
+    delta += pow(ctlat * sdlong,2);
+    delta = sqrt(delta);
+    denom = (sflat * stlat) + (cflat * ctlat * cdlong);
+    delta = atan2(delta, denom);
+    distance = delta * 6372795;
+    return distance;
+}
+
 
 //////////////////////////////////////////////////////////////
 //Shutdown Function
@@ -249,11 +390,12 @@
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     //UPDATE THIS TO WORK WITH ST RTC INSTEAD
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ /*  
     if(RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) {
         pbKill = 0; // this is were we shut everything down
         //pc.printf("If you're reading this something has gone very wrong.");
     }
-    
+*/ 
 }
 
 //////////////////////////////////////////////////////////////
@@ -262,17 +404,80 @@
 void log_data()
 {
 
-    RGB_LED.set_led(1,1,0);
+    //RGB_LED.set_led(1,1,0);
         
     time_t seconds = time(NULL);
     strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
+/*
     RTC_UPAS.get_time(); 
     press = bmesensor.getPressure();
     temp = bmesensor.getTemperature()-5.0;
     rh = bmesensor.getHumidity();
     uv =  lightsensor.getUV();
         
-    
+    gps.read(1);
+        
+        gpsspeed = gps.speed;
+        gpssatellites =  gps.satellites;
+        gpslatitude = gps.lat;
+       // gpslat = 'N'; //gps.lat; need to fix this (if statement?)
+        gpslongitude = gps.lon;
+       // gpslon = 'W'; //gps.lon; need to fix this (if statement?)
+        gpsaltitude = gps.altitude;
+        
+                    if (abs(gpslatitude) > 0 && abs(gpslongitude) > 0) {
+      
+            if(gpslat == 'S')
+              {
+              flat = convertDegMinToDecDeg (gpslatitude) * -1;
+              }
+              else
+              {
+              flat = convertDegMinToDecDeg (gpslatitude);
+              }
+              
+              if(gpslon == 'W')
+              {
+              flon = convertDegMinToDecDeg (gpslongitude) * -1;
+              }
+              else
+              {
+              flon = convertDegMinToDecDeg (gpslongitude);
+              }
+          
+                workDistance = GPSdistanceCalc (work_lat, work_lon);
+                homeDistance = GPSdistanceCalc (home_lat, home_lon);
+              }
+              
+              if (homeDistance == 99999 && workDistance == 99999) {
+            //    digitalWrite (work_yellow_led, HIGH);
+            //    digitalWrite (home_green_led, HIGH);
+            //    digitalWrite (travel_red_led, HIGH);
+                location = 0;
+              }
+              
+              else if (workDistance < 30) {
+            //    digitalWrite (work_yellow_led, HIGH);
+            //    digitalWrite (home_green_led, LOW);
+            //    digitalWrite (travel_red_led, LOW);
+                location = 1;
+              }
+               
+              else if (homeDistance < 20) {
+            //    digitalWrite (work_yellow_led, LOW);
+            //    digitalWrite (home_green_led, HIGH);
+            //    digitalWrite (travel_red_led, LOW);
+                location = 2;
+              }
+              
+              else {
+            //    digitalWrite (work_yellow_led, LOW);
+            //    digitalWrite (home_green_led, LOW);
+            //    digitalWrite (travel_red_led, HIGH);
+                location = 3;
+              }
+
+     
     FILE *fp = fopen(filename, "a");
     fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC_UPAS.year, RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds);
     fprintf(fp, "%s,", timestr);
@@ -280,10 +485,15 @@
     fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp);
     fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass);
     fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps());
-    fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
+    fprintf(fp, "%d,%d,%d,%1.3f,%1.3f,", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
+    fprintf(fp, "%.4f,%.4f,%06d,%06d,%f,%d,%f,\r\n",  gps.lat, gps.lon, (long)gps.date, (long)gps.utc, gpsspeed, gpssatellites,  gpsaltitude); // test and add in speed, etc that Josh added in to match the adafruit GPS
     fclose(fp);
     free(fp);
     RGB_LED.set_led(1,0,0);
+*/
+
+pc.printf("%s,", timestr);
+
 
 }
 
@@ -292,6 +502,7 @@
 //////////////////////////////////////////////////////////////
 void flowControl()
 {
+/*        
         RGB_LED.set_led(0,1,0);
         omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
         omronVolt = (omronReading*4.096)/(32768*2);
@@ -310,46 +521,60 @@
         deltaVflow = volflow-volflowSet;
         massflowSet = volflowSet*atmoRho;
         deltaMflow = massflow-massflowSet;
+        
         if(abs(deltaMflow)>.025) {
             digital_pot_change = (int)(gainFlow*deltaMflow);
     
     
-            if(abs(digital_pot_change)>=50) {
-                digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
-                //RGB_LED.set_led(1,0,0);
-            }   else {
+            if(abs(digital_pot_change)>=10) {
+                digital_pot_set = (int)(digital_pot_set+ (int)(1*deltaMflow));
+                RGB_LED.set_led(1,0,0);
+            } else {
                 digital_pot_set = (digital_pot_set+ digital_pot_change);
-               // RGB_LED.set_led(1,1,0);
+                RGB_LED.set_led(1,1,0);
             }
             
-                    if(digital_pot_set>=digitalpotMax) {
-                        digital_pot_set = digitalpotMax;
-                        //RGB_LED.set_led(1,0,0);
-                    } else if(digital_pot_set<=digitalpotMin) {
-                        digital_pot_set = digitalpotMin;
-                        //RGB_LED.set_led(1,0,0);
-                    }
-    
+            if(digital_pot_set>=digitalpotMax) {
+                 digital_pot_set = digitalpotMax;
+                 RGB_LED.set_led(1,0,0);
+            } else if(digital_pot_set<=digitalpotMin) {
+                 digital_pot_set = digitalpotMin;
+                 RGB_LED.set_led(1,0,0);
+            }
     
             DigPot.writeRegister(digital_pot_set);
-    
+                
         } else {
-            //RGB_LED.set_led(0,1,0);
+            RGB_LED.set_led(0,1,0);
         }
+        
+*/
 }
-
 //////////////////////////////////////////////////////////////
 //Main Function
 //////////////////////////////////////////////////////////////
 int main(){
 
-    //wait(10);
-    //RGB_LED.set_led(0,0,1);
+/*
+    motor1.drive(254); //closed = 253, open = 254
+    motor2.drive(253); //closed = 253, open = 254
+    motor3.drive(253); //closed = 253, open = 254
+    motor4.drive(253); //closed = 253, open = 254
+    
+    wait(1);
+    motor1.stop();
+    motor2.stop();
+    motor3.stop();
+    motor4.stop();
+*/
+
     pc.baud(115200);  // set what you want here depending on your terminal program speed
     pc.printf("\f\n\r-------------Startup-------------\n\r");
     wait(0.5);
     
+    
     uint8_t serialNumberAndType[6] = {0x50,0x53};
+    /*
     E2PROM.read(0x00034,serialNumberAndType+2,2);
    
     int tempSerialNum = serialNumberAndType[2]+serialNumberAndType[3];
@@ -363,7 +588,9 @@
     serialNumberAndType[3] = serialNumDigits[1]+48;
     serialNumberAndType[4] = serialNumDigits[2]+48;
     serialNumberAndType[5] = serialNumDigits[3]+48;
+    
     RGB_LED.set_led(0,1,0);
+    */
     pc.attach(pc_recv);
     microChannel.attach(uartMicro,microChannel.RxIrq);
     microChannel.baud(115200);  
@@ -378,32 +605,29 @@
     microChannel.printf("---\r");
     wait(0.5);
     
-
-       
-    
-    RGB_LED.set_led(1,1,1);
+    //RGB_LED.set_led(1,1,1);
     while(runReady!=1) {
         wait(1);
         pc.printf("Waiting for BLE instruction");
     
     }
     
-
-    
+/*   
     E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
     RGB_LED.set_led(0,1,0);
-    
+*/    
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     //UPDATE THIS TO WORK WITH ST RTC INSTEAD
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-    
+/*
     while(!RTC_UPAS.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) {  // this while waits for the start time by looping until the start time
             wait(0.5);
             
             RTC_UPAS.get_time(); 
 
     }
-    
+*/
+/*
     
     //Get the proper serial number
     uint8_t serialBytes[2] = {0,};
@@ -415,7 +639,6 @@
     E2PROM.read(0x00014,logByte,1);
     logInerval = logByte[0];
     
-   
     //Use the flow rate value stored in eeprom
     uint8_t flowRateBytes[4] = {0,};
     E2PROM.read(0x00010,flowRateBytes,4);
@@ -437,28 +660,79 @@
     atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
     massflowSet = volflowSet*atmoRho;
 
-
     DigPot.writeRegister(digital_pot_setpoint);
     wait(1);
-    //blower = 1;
+    blower = 1;
 
     uint8_t subjectLabelOriginal[8] = {0,};
     E2PROM.read(0x00001, subjectLabelOriginal,8); 
-    
+
+*/
+        
     time_t seconds = time(NULL);
     strftime(timestr, 32, "%y-%m-%d-%H=%M=%S", localtime(&seconds));
+/*
     //sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%c%c%c%c%c%c%c%c.txt",serial_num,RTC_UPAS.year,RTC_UPAS.month,RTC_UPAS.date,RTC_UPAS.hour,RTC_UPAS.minutes,RTC_UPAS.seconds,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]);
-    sprintf(filename, "/sd/UPASboardtest_%s.txt", timestr);
+    sprintf(filename, "/sd/UPAS_TboardtestLog_%s_%c%c%c%c%c%c%c%c.txt", timestr,subjectLabelOriginal[0],subjectLabelOriginal[1],subjectLabelOriginal[2],subjectLabelOriginal[3],subjectLabelOriginal[4],subjectLabelOriginal[5],subjectLabelOriginal[6],subjectLabelOriginal[7]);
     FILE *fp = fopen(filename, "w");
     fclose(fp);
 
 
+    omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
+    omronVolt = (omronReading*4.096)/(32768*2);
+    if(omronVolt<=calibrations.omronVMin) {
+        massflow = calibrations.omronMFMin;
+    } else if(omronVolt>=calibrations.omronVMax) {
+        massflow = calibrations.omronMFMax;
+    } else {
+        massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
+    }
+    deltaMflow = massflow-massflowSet;
+    digital_pot_set = digital_pot_setpoint;
+    wait(5);
+
+    //---------------------------------------------------------------------------------------------//
+    //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
+
+    while(abs(deltaMflow)>.025) {
+
+        omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
+        omronVolt = (omronReading*4.096)/(32768*2);
+        //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
+        if(omronVolt<=calibrations.omronVMin) {
+            massflow = calibrations.omronMFMin;
+        } else if(omronVolt>=calibrations.omronVMax) {
+            massflow = calibrations.omronMFMax;
+        } else {
+            massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
+        }
+
+        atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
+        volflow = massflow/atmoRho;
+        massflowSet = volflowSet*atmoRho;
+        deltaMflow = massflow-massflowSet;
+
+        digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
+        if(digital_pot_set>=digitalpotMax) {
+            digital_pot_set = digitalpotMax;
+        } else if(digital_pot_set<=digitalpotMin) {
+            digital_pot_set = digitalpotMin;
+        }
+
+        wait(2);
+        DigPot.writeRegister(digital_pot_set);
+        pc.printf("%d,\r\n", digital_pot_set);
+        wait(1);
+
+
+    }
+
     sampledVol = 0.0;
     RGB_LED.set_led(0,1,0);
-
-    stop.attach(&check_stop, 9);    // check if we should shut down every 9 number seconds, starting after the start.
+*/
+  //  stop.attach(&check_stop, 9);    // check if we should shut down every 9 number seconds, starting after the start.
     logg.attach(&log_data, logInerval);
-    flowCtl.attach(&flowControl, 1);
+    //flowCtl.attach(&flowControl, 1);
             
 
     //** end of initalization **//