Implement a SD card into HW6

Dependencies:   SDFileSystem mbed

Fork of shomberg_hw_6 by Russell Shomberg

Revision:
19:1f4dd59bbe6b
Parent:
18:699b41309be7
Child:
20:fc1690076f87
diff -r 699b41309be7 -r 1f4dd59bbe6b main.cpp
--- a/main.cpp	Wed Oct 31 22:50:37 2018 +0000
+++ b/main.cpp	Wed Oct 31 23:32:01 2018 +0000
@@ -1,5 +1,5 @@
 /**
-    Temperature Sensor for hw6
+    Temperature Sensor and Logging for hw7
     main.cpp
 
     Purpose:    Read signal from TMP36 connected to pin20
@@ -32,41 +32,75 @@
 float current_time;
 float current_voltage;
 float current_temp;
+int file_counter=0;
+char file_name [20];
+Timer t;
+FILE *fp;
+
+// Initialize SDFileSystem
+SDFileSystem sd(p12,p13,p14,p15,"sd");
 
 
 
 int main()
 {
+    //Initialize Timer
+    t.start();
+
+    // Mount the sd and
+    printf("Mounting SD Card\n\r");
+    sd.mount();
 
     while(1) {
         while(read_switch()) {  // skip everything if switch is off
-            // Take initial readings
-            starting_voltage = read_sensor();
-            starting_temp = onvert_mV_to_temp(starting_voltage);
 
             // 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");
 
-            // Write header to file
-            //"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r"
+            if (fp!=NULL) { // Don't procede if there is an issue
 
-            // start a loop that logging
-            // exiting this loop will initialize data save procedures
-            while(read_switch()) { // Main loop for logging
+                // Write header to file
+                fprintf(fp,"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r");
+
+                // Take initial readings
+                starting_voltage = read_sensor();
+                starting_temp = convert_mV_to_temp(starting_voltage);
 
-                // Get data for logging
-                current_time = check_time();
-                current_voltage = read_sensor();
-                current_temp = convert_mV_to_temp(current_voltage);
+                // start a loop that logging
+                // exiting this loop will initialize data save procedures
+                while(read_switch()) { // Main loop for logging
+                    printf("Logging data....\n\r");
+                    // Get data for logging
+                    current_time = t.read();
+                    current_voltage = read_sensor();
+                    current_temp = convert_mV_to_temp(current_voltage);
 
-                //"%1.2f, %1.2f, %1.2f, %1.2f \n\r", current_time, current_voltage, current_temp, current_temp-starting_temp
-                
-                wait(1);
+                    fprintf(fp,"%1.2f, %1.2f, %1.2f, %1.2f \n\r", current_time, current_voltage, current_temp, current_temp-starting_temp);
+
+                    wait(1);
+                }
+            } else { // runs if there was a issue opening the file
+                printf("Issue with opening file\n\r");
             }
+
             // This code runs when the switch is toggled off
             // Close out file
-            
+            printf("Logging complete! \n\r Saving file \n\r");
+            fclose(fp);
+
             // Print to USB serial
-
+            printf("Uploading data to USB");
+            //
+            fp = fopen(file_name,"r");
+            if (fp!=NULL) { // Don't procede if there is an issue
+                printf("I don't know how to do this");
+            } else { // runs if there was a issue opening the file
+                printf("Issue with opening file\n\r");
+                fclose(fp);
+                file_counter++;
+            }
         }
         // Re-enter main while loop
         //continuously check for switch to turn on again