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:
7:29b01d5812ee
Parent:
6:a738dcd53bf8
Child:
8:c4a8f9b67cee
--- a/main.cpp	Tue Mar 01 02:56:20 2016 +0000
+++ b/main.cpp	Tue Mar 01 16:59:14 2016 +0000
@@ -10,11 +10,11 @@
 #include "CronoDot.h"
 #include "EEPROM.h"
 #include "Calibration.h" 
-Serial pc(USBTX, USBRX);
+//Serial pc(USBTX, USBRX);
 //Serial microChannel(D0, D1); // tx, rx Appears there is a conflict in the mbed code (possibly already assigned to SERIAL_TX, SERIAL_RX, USBTX, USBRX need to reassign these values)
 // However still an issue with the BLE not reading the EEPROM with the old pin assignments Jake had.
 Serial microChannel(D8, D2); // tx, rx
-Timer t;
+//Timer t;
 struct tm tt;
 
 I2C                 i2c(D14, D15);
@@ -35,14 +35,14 @@
 Calibration         calibrations(1);     //Default serial/calibration if there are no values for the selected option
 
 Timeout         stop;   //This is the stop call back object
-Timeout         logg;   //This is the logging call back object
+//Timeout         logg;   
+Ticker          logg;     //This is the logging call back object
 
 uint16_t serial_num = 1;                // Default serial/calibration number
 int RunReady =0;
 
 struct tm STtime;
-
-
+char timestr[32];
 
 float press = 1.1;
 float temp = 75.5;
@@ -94,10 +94,11 @@
 
 // variables are only place holders for the US_Menu //
 int refreshtime = 6;
+//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;
 //*************************************************//
 
-//int refresh_Time = 10;   // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
+
 
 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
 
@@ -105,13 +106,6 @@
 
 void sendData(); 
 
-//int timeout = 2;
-
-//void pc_recv(void){
-//    while(pc.readable()){
-//        pc.getc();
-//    }
-//}
 
 static uint8_t rx_buf[20];
 static uint8_t rx_len=0;
@@ -168,7 +162,19 @@
             
             }else if(transmissionValue ==1){ //process and store RTC values
                 
-                if(dataLength==6)RTC_UPAS.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
+                //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
+                        //sets ST RTC
+                        STtime.tm_sec = writeData[0];    // 0-59
+                        STtime.tm_min = writeData[1];    // 0-59
+                        STtime.tm_hour = writeData[2];   // 0-23
+                        STtime.tm_mday = writeData[3];   // 1-31
+                        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
+                    }
     
             }else if(transmissionValue ==2){ //process and store sample start/end 
                 if(dataLength ==12)E2PROM.write(0x00015, writeData, 12);
@@ -229,6 +235,7 @@
     
 } 
 
+
 void check_stop()   // this checks if it's time to stop and shutdown
 {
     
@@ -245,27 +252,39 @@
 void log_data()
 {
 
-
+        RGB_LED.set_led(1,1,0);
+        
+    time_t seconds = time(NULL);
+    strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
+    press = bmesensor.getPressure();
+    temp = bmesensor.getTemperature()-5.0;
+    rh = bmesensor.getHumidity();
+    uv =  lightsensor.getUV();
+        
+    
+    FILE *fp = fopen(filename, "a");
+    //fprintf(fp, "Time as a basic string = %s\r\n", ctime(&seconds));
+    //fprintf(fp, "Time as a basic string = %s\r\n", timestr);
+    //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);
+    fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
+    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);
+    fclose(fp);
+    free(fp);
+    RGB_LED.set_led(1,0,0);
 
 }
  
 int main(){
-    //RGB_LED.set_led(1,1,1);
-    STtime.tm_sec = 10;    // 0-59
-    STtime.tm_min = 38;    // 0-59
-    STtime.tm_hour = 15;   // 0-23
-    STtime.tm_mday = 26;   // 1-31
-    STtime.tm_mon = 1;     // 0-11
-    STtime.tm_year = 116;  // year since 1900
-    time_t STseconds = mktime(&STtime);
-    set_time(STseconds); // Set RTC time to 16 December 2013 10:05:23 UTC
-    char timestr[32];
+
     //wait(10);
     //RGB_LED.set_led(0,0,1);
     //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);
-    //timeout=2;
     
     uint8_t serialNumberAndType[6] = {0x50,0x53};
     E2PROM.read(0x00034,serialNumberAndType+2,2);
@@ -307,11 +326,16 @@
     
     }
     
-    
+
     //wait(1);
     
     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);
             
@@ -330,8 +354,7 @@
     E2PROM.read(0x00014,logByte,1);
     logInerval = logByte[0];
     
-    stop.attach(&check_stop, 30);    // check if we should shut down every 9 seconds, starting 60s after the start.
- 
+   
     //Use the flow rate value stored in eeprom
     uint8_t flowRateBytes[4] = {0,};
     E2PROM.read(0x00010,flowRateBytes,4);
@@ -374,48 +397,25 @@
     //---------------------------------------------------------------------------------------------//
     //Following lines are needed to enter into the initiallization flow control loop
 
-    wait(5);
-
 
     sampledVol = 0.0;
     RGB_LED.set_led(0,1,0);
-
-
+    //log_data();
+    stop.attach(&check_stop, 31);    // check if we should shut down every 9 seconds, starting 60s after the start.
+    logg.attach(&log_data, logInerval);
+            
 
     //** end of initalization **//
     //---------------------------------------------------------------------------------------------//
     //---------------------------------------------------------------------------------------------//
     // Main Control Loop
 
-
+    while (1) {
+        // Do other things...
+    }
+    
     //logg.attach(&log_data, 30); // uses callbacks or block Interrupts for anything that uses i2c
    // while(!RTC_UPAS.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])){
-      while(1){   
-        wait(logInerval);
-        RGB_LED.set_led(1,1,0);
-        
-        
-    time_t seconds = time(NULL);
-    strftime(timestr, 32, "%y%m%d%H%M%S", localtime(&seconds));
-    press = bmesensor.getPressure();
-    temp = bmesensor.getTemperature()-5.0;
-    rh = bmesensor.getHumidity();
-    uv =  lightsensor.getUV();
-        
-    FILE *fp = fopen(filename, "a");
-    //fprintf(fp, "Time as a basic string = %s\r\n", ctime(&seconds));
-    //fprintf(fp, "Time as a basic string = %s\r\n", timestr);
-    //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);
-    fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
-    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);
-    fclose(fp);
-    free(fp);
-    RGB_LED.set_led(1,0,0);
-    }
 
 
 }