Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017

Dependencies:   ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822

Fork of UPAS_BLE_and_USB by Volckens Group Sensors

Revision:
67:300418575137
Parent:
64:2906b0c48403
Child:
68:ca444d0760a0
--- a/main.cpp	Fri Aug 14 21:44:13 2015 +0000
+++ b/main.cpp	Sat Aug 15 02:10:49 2015 +0000
@@ -9,7 +9,7 @@
 #include "NCP5623BMUTBG.h"
 #include "CronoDot.h"
 #include "EEPROM.h"
-#include "UPAS_Serial.h"
+#include "Honduras_SerialMenu.h"
 
 #define SERIAL_BAUD_RATE    9600
 #define SCL         20
@@ -29,10 +29,13 @@
 NCP5623BMUTBG RGB_LED(p22, p20);
 CronoDot RTC(p22, p20);
 EEPROM          E2PROM(p22, p20);
-UPAS_Serial     Menu;
+Honduras_SerialMenu     Menu;
+DigitalOut      GPS_EN(p4,0);                     //pin 4 is used to enable and disable the GPS, in order to recive serial communications
 
 Timeout         stop;   //This is the stop call back object
 Timeout         logg;    //This is the logging call back object
+int RunReady =0;
+
 
 //UPAS0012 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx'
 //mass flow sensor output signal (x) vs. mass flow (y)
@@ -63,6 +66,7 @@
 int vis;
 int ir;
 
+float compass;
 float accel_x;
 float accel_y;
 float accel_z;
@@ -80,7 +84,7 @@
 
 float massflow; //g/min
 float volflow; //L/min
-float volflowSet = 2.0; //L/min
+float volflowSet = 1.0; //L/min
 int   logInerval = 10;
 double secondsD = 0;
 float massflowSet;
@@ -97,8 +101,9 @@
 
 int dutyUp;
 int dutyDown;
+uint8_t f_sec, f_min, f_hour, f_date, f_month, f_year;
 
-int refresh_Time = 10;   // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
+//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 device_name[] = "---------------";
 char filename[] = "/sd/UPAS0012LOG000000000000---------------.txt";
@@ -109,7 +114,7 @@
 {
     //RTC.get_time(); //debug
     //pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig
-    if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) {
+    if(RTC.compare(f_sec, f_min, f_hour, f_date, f_month, f_year)) {
         pbKill = 0; // this is were we shut everything down
     }
     stop.detach();
@@ -165,6 +170,7 @@
 
     movementsensor.getACCEL();
     movementsensor.getCOMPASS();
+    compass = movementsensor.getCOMPASS_HEADING();
     accel_x = movementsensor.AccelData.x;
     accel_y = movementsensor.AccelData.y;
     accel_z = movementsensor.AccelData.z;
@@ -186,7 +192,7 @@
     //Mount the filesystem
     //sd.mount();
     FILE *fp = fopen(filename, "a");
-    fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%1.3f,%1.3f\r\n",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,omronVolt,massflow,temp,press,rh,atmoRho,volflow,sampledVol,accel_x,accel_y,accel_z,accel_comp,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps(), gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
+    fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%1.3f,%1.3f,%f\r\n",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,omronVolt,massflow,temp,press,rh,atmoRho,volflow,sampledVol,accel_x,accel_y,accel_z,accel_comp,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps(), gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow, compass);
     fclose(fp);
     //Unmount the filesystem
     //sd.unmount();
@@ -202,34 +208,32 @@
 // Setup and Initialization
 //---------------------------------------------------------------------------------------------//
 
-    RGB_LED.set_led(0,1,1);
+    
 
-    Menu.read_menu(E2PROM, refresh_Time, volflowSet, device_name, dutyUp, dutyDown);       //Read all data from the EEPROM here
-fix_error:
-    Menu.Start(pc, E2PROM, RTC, refresh_Time, volflowSet, device_name, dutyUp, dutyDown);  //Forces you to open the menu
-    Menu.save_menu(E2PROM, refresh_Time, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
-
+    Menu.read_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Read all data from the EEPROM here
+    
     //Test for errors
     if(RTC.OSF()) {                 //Don't proceed if the RTC needs reset
-        RGB_LED.set_led(1,0,0); // error code/color
+        RGB_LED.set_led(0,1,1); // error code/color
         pc.printf("DATE/TIMESTAMP NEEDS TO BE RECALIBRATED!!\r\n");
-        goto fix_error;
+        Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown);  //Forces you to open the menu
+        Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
     }
-    RTC.get_time();
-    if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) {  //Don't proceed if it's already time to stop
-        RGB_LED.set_led(1,0,0); // error code/color
-        pc.printf("Please fix the START or STOP times before exiting!\r\n");
-        goto fix_error;
-    }
-    pc.printf("You're done, you can now disconect the USB cable.");
+    
+    if(RunReady == 0){
+            RGB_LED.set_led(0,1,1); // error code/color
+            pc.printf("Change Name\r\n");
+            Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown);  //Forces you to open the menu
+            RunReady = 1;
+            Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
+            pbKill = 0;
+            }
+            
+      RunReady = 0;
+      Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM      
 
-    RGB_LED.set_led(1,1,0);
-    while(!RTC.compare(Menu.s_sec, Menu.s_min, Menu.s_hour, Menu.s_day, Menu.s_month, Menu.s_year)) {  // this while waits for the start time by looping until the start time
-        // serial print a count down??
-    }
     stop.attach(&check_stop, 60);    // check if we should shut down every 5 seconds, starting 60s after the start.
 
-
     if(volflowSet==1.0) {
         gainFlow = 100;
     } else if(volflowSet==2.0) {
@@ -259,6 +263,61 @@
     blower = 1;
 
     RTC.get_time();
+    f_sec = RTC.seconds;
+    f_min = RTC.minutes;
+    f_hour = RTC.hour;
+    if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10){
+        if(RTC.date == 31){
+            f_date = 1;
+            f_month = RTC.month +1;
+            f_year = RTC.year; 
+            }
+        else{
+            f_date = RTC.date+1;
+            f_month = RTC.month;
+            f_year = RTC.year;
+            }
+        }
+    else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11){
+        if(RTC.date == 30){
+            f_date = 1;
+            f_month = RTC.month +1;
+            f_year = RTC.year; 
+            }
+        else{
+            f_date = RTC.date+1;
+            f_month = RTC.month;
+            f_year = RTC.year;
+            }
+        }
+    else if(RTC.month == 2){
+        if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28){
+            if(RTC.date == 29){
+                f_date = 1;
+                f_month = RTC.month +1;
+                f_year = RTC.year; 
+                }
+            else{
+                f_date = RTC.date+1;
+                f_month = RTC.month;
+                f_year = RTC.year;
+                }
+            }
+        else{
+            if(RTC.date == 28){
+                f_date = 1;
+                f_month = RTC.month +1;
+                f_year = RTC.year; 
+                }
+            else{
+                f_date = RTC.date+1;
+                f_month = RTC.month;
+                f_year = RTC.year;
+                }
+        }
+        }
+   
+    
     sprintf(filename, "/sd/UPAS0012LOG %02d-%02d-%02d %02d=%02d=%02d %s.txt",RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name);
     FILE *fp = fopen(filename, "w");
     fclose(fp);
@@ -286,7 +345,7 @@
     //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
 
     while(abs(deltaMflow)>.015) {
-
+    
         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
@@ -303,6 +362,7 @@
         massflowSet = volflowSet*atmoRho;
         deltaMflow = massflow-massflowSet;
         //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set);
+        pc.printf("%d,%d,%d,%d,%d,%d\r\n",f_sec,f_min,f_hour,f_date,f_month,f_year,digital_pot_set);
         digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
         if(digital_pot_set>=digitalpotMax) {
             digital_pot_set = digitalpotMax;
@@ -319,6 +379,8 @@
 
     sampledVol = 0.0;
     RGB_LED.set_led(0,1,0);
+    
+
 
     //** end of initalization **//