mbed Starter Kit / mbed Starter Kit Demo Programs
Revision:
0:6a73d3dc037e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/temp_logging/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,72 @@
+// Temperature logging demo - record temperatures to SD card and print them to
+// the console every 10 seconds
+
+#include "mbed.h"
+#include "SDFileSystem.h"
+
+// Analog input (pin 15)
+AnalogIn ain(p15);
+
+// USB serial (tx, rx)
+Serial pc(USBTX, USBRX);
+
+// SD card (SPI pins)
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+
+// Timer for our timestamps
+Timer timer;
+
+int main() {
+    
+    FILE *file;
+    float voltage_in;
+    float degrees_c;
+    int i;
+    int c;
+    
+    // Start our timer
+    timer.start();
+        
+    // Open file for writing
+    file = fopen("/sd/temp_data.txt", "w");
+    if ( file == NULL ) {
+        error("ERROR: Could not open file for writing!\n");
+        return -1;
+    }
+    
+    // Tell the user we need to wait while we collect some data
+    pc.printf("\nCollecting data (Do not remove SD Card!) ...\n");
+    
+    // Collect temperatures with timestamps every second
+    for(i = 0; i < 10; i++) {
+        voltage_in = ain * 3.3;
+        degrees_c = (voltage_in - 0.5) * 100.0;
+        fprintf(file, "%2.2fs: %3.1f deg C\n", timer.read(), degrees_c);
+        wait(1);
+    }
+    
+    // Close file and re-open it for reading
+    fclose(file);
+    file = fopen("/sd/temp_data.txt", "r");
+    if ( file == NULL ) {
+        error("ERROR: Could not open file for reading!\n");
+        return -1;
+    }
+    
+    // Print results to console
+    printf("Temperature data:\n");
+    while(1) {
+       c = fgetc(file);
+       if ( c == EOF ) {
+           break;
+       }
+       pc.putc(c);
+    }
+    
+    // Close the file and finish
+    fclose(file);
+    pc.printf("Done!\n");
+    
+    return 0;
+}
+ 
\ No newline at end of file