Use accelerometer to interrupt.

Dependencies:   mbed SDFileSystem

Fork of shomberg_hw_7 by Russell Shomberg

Revision:
23:61d87ea09c26
Parent:
20:fc1690076f87
Child:
24:9264fbd225d0
diff -r c3c335503260 -r 61d87ea09c26 main.cpp
--- a/main.cpp	Tue Nov 06 14:41:56 2018 +0000
+++ b/main.cpp	Wed Nov 07 15:29:47 2018 +0000
@@ -6,114 +6,88 @@
                 Toggle switch connected to pin07 activates SD logging
                 Log: Time Elasped, Current Voltage, Current Temp, Delta Temp
                 Finish log -> save file and send to USB serial port
+                if accelerometer records transient, 3 sec of data is recorded
 
     @author  Russell Shomberg
-    @created 2018-10-31
-    @revised 2018-10-31
+    @created 2018-11=01
+    @revised 2018-11-06
     @version 0.0
 
-    Issues: SD Card Writing functions need to be in a seperate file
+    Issues:
 
 */
 
+
 // INCLUDES
 #include "mbed.h"
+#include "data_log.h"
 #include "OCE360Input.h"
 #include "SDFileSystem.h"
+#include "MMA8452Q.h"
 
 // OUTPUTS
 Serial pc(USBTX, USBRX);        // for debugging
+DigitalOut led(LED1);
 
 // VARIABLES
-int outputT= 0;
-float starting_voltage;
+Timer t;
+Timer debounce;
 float starting_temp;
 
-float current_time;
-float current_voltage;
-float current_temp;
-int file_counter=0;
-char file_name [20];
-Timer t;
-FILE *fp;
-unsigned char c;                          // a single byte buffer
+InterruptIn accel_interrupt(PIN_ACCEL_I1);
+
+void toggle(void);
+void log_data(void);
+Ticker data_logger;
+Ticker accel_logger;
+
 
 // Initialize SDFileSystem
-SDFileSystem sd(p5,p6,p7,p8,"sd");
-
-
+SDFileSystem sd(PIN_SD_MOSI,PIN_SD_MISO,PIN_SD_SCLK,PIN_SD_CS,"sd");
 
 int main()
 {
+    // Take initial reading
+    starting_temp = convert_mV_to_temp(read_sensor());
+
     //Initialize Timer
     t.start();
+    debounce.start();
 
-    // Mount the sd and
-    printf("\n\r\n\rMounting SD Card\n\r");
+    // Mount the sd
     sd.mount();
 
+    // Initialize Accelerometer
+    accel_interrupt.rise(&toggle);
+
     while(1) {
-        while(!read_switch()) {}
-
-        while(read_switch()) {  // skip everything if switch is off
-
-            // Initialize a file
-            printf("Opening a new file: temp_log%d.csv\n\r",file_counter);
-            sprintf(file_name,"/sd/temp_log%d.csv",file_counter);
-            fp = fopen(file_name,"w");
-
-            if (fp!=NULL) { // Don't procede if there is an issue
+        while(!read_switch()) {
+            wait(1);   // nothing happens when the switch is off
+        }
 
-                // Write header to file
-                fprintf(fp,"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r");
+        temp_log tempLog = temp_log(starting_temp);
 
-                // Take initial readings
-                starting_voltage = read_sensor();
-                starting_temp = convert_mV_to_temp(starting_voltage);
-
-                // start a loop that logging
-                // exiting this loop will initialize data save procedures
-                printf("Logging data....\n\r");
-                while(read_switch()) { // Main loop for logging
+        data_logger.attach(&toggle,.1);
 
-                    // Get data for logging
-                    current_time = t.read();
-                    current_voltage = read_sensor();
-                    current_temp = convert_mV_to_temp(current_voltage);
-
-                    fprintf(fp,"%1.2f, %1.2f, %1.2f, %1.2f \n\r", current_time, current_voltage, current_temp, current_temp-starting_temp);
-
-                    wait(1);
-                }
+        // start a loop that logging
+        // exiting this loop will initialize data save procedures
 
-                // This code runs when the switch is toggled off
-                // Close out file
-                printf("Logging complete! \n\r Saving file \n\r");
-                fclose(fp);
+        while(read_switch()) { // Main loop for logging
+            tempLog.write_data(t.read());
+            wait(.5);
 
-                // Print to USB serial
-                printf("Uploading data to USB \n\r");
-                //
-                fp = fopen(file_name,"r");
-                if (fp!=NULL) { // Don't procede if there is an issue
-                    while (!feof(fp)) {                       // while not end of file
-                        c=fgetc(fp);                         // get a character/byte from the file
-                        printf("%c",c); // and show it in hex format
-                    }
-                } else { // runs if there was a issue opening the file
-                    printf("Issue with opening file\n\r");
-                    fclose(fp);
-                }
+        }
+
+        tempLog.write_to_pc();
+        //~tempLog();
+    }
 
 
-            } else { // runs if there was a issue opening the file
-                printf("Issue with opening file\n\r");
-            }
-        }
+
+}
 
-        // Re-enter main while loop
-        //continuously check for switch to turn on again
-        file_counter++;
-        printf("waiting...");
-    }
-}
\ No newline at end of file
+void toggle()
+{
+    led=!led;
+}
+