Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem
main.cpp
00001 /* Program demonstrating data logging to SD card 00002 Temperature is read in from an TMP36 00003 The time since program start and the voltage 00004 are written to a tab-delimited data file. 00005 TMP36 pins: left = power, middle = output (p18), right = ground 00006 SD pins: DI = p5, DO = p6, SCK = p7, CS = p8 00007 */ 00008 #include "mbed.h" 00009 #include "SDFileSystem.h" 00010 00011 AnalogIn tmp36(p18); 00012 Serial pc(USBTX,USBRX); 00013 SDFileSystem fs(p5, p6, p7, p8, "fs"); 00014 Timer t; 00015 Ticker sampleTime; 00016 DigitalOut doneLED(LED1); 00017 bool timeToRead; 00018 00019 void triggerCollection() { 00020 timeToRead = true; // flag to collect data 00021 } 00022 00023 int main() { 00024 00025 float voltage, temperature; 00026 // Mount the filesystem 00027 bool mountFailure = fs.mount(); 00028 if (mountFailure != 0) { 00029 pc.printf("Failed to mount the SD card.\r\n"); 00030 return -1; // ends program with error status 00031 } 00032 00033 FILE* fp = fopen("/fs/log.txt","w"); 00034 if (fp == NULL) { 00035 pc.printf("Failed to open the file.\r\n"); 00036 fs.unmount(); 00037 return -1; 00038 } 00039 00040 // Write a header row 00041 fprintf(fp, "Time (s) \t Temperature (deg C)\r\n"); 00042 00043 // Start the timer and ticker 00044 t.start(); 00045 sampleTime.attach(&triggerCollection, 0.5); // write data every 0.5 s 00046 timeToRead = true; // collect data at t = 0 00047 00048 while (t.read()<20) { 00049 if (timeToRead) { 00050 timeToRead = false; // reset data collection flag 00051 voltage = 3.3*tmp36.read(); 00052 temperature = (voltage-0.5)/0.01; 00053 fprintf(fp,"%.2f \t %.2f\r\n", t.read(),temperature); 00054 } 00055 } 00056 00057 // Close the file and unmount the file system so the SD card is happy 00058 fclose(fp); 00059 fs.unmount(); 00060 // Turn on LED to let user know it is safe to remove the SD card 00061 doneLED = 1; 00062 }
Generated on Tue Jul 19 2022 01:00:07 by
1.7.2