logging temp with sd card
Dependencies: mbed SDFileSystem
main.cpp@0:9dfb24edf079, 2019-03-19 (annotated)
- Committer:
- jschilling22
- Date:
- Tue Mar 19 16:45:48 2019 +0000
- Revision:
- 0:9dfb24edf079
logging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jschilling22 | 0:9dfb24edf079 | 1 | /* Program demonstrating data logging to SD card |
jschilling22 | 0:9dfb24edf079 | 2 | Temperature is read in from an TMP36 |
jschilling22 | 0:9dfb24edf079 | 3 | The time since program start and the voltage |
jschilling22 | 0:9dfb24edf079 | 4 | are written to a tab-delimited data file. |
jschilling22 | 0:9dfb24edf079 | 5 | TMP36 pins: left = power, middle = output (p18), right = ground |
jschilling22 | 0:9dfb24edf079 | 6 | SD pins: DI = p5, DO = p6, SCK = p7, CS = p8 |
jschilling22 | 0:9dfb24edf079 | 7 | */ |
jschilling22 | 0:9dfb24edf079 | 8 | #include "mbed.h" |
jschilling22 | 0:9dfb24edf079 | 9 | #include "SDFileSystem.h" |
jschilling22 | 0:9dfb24edf079 | 10 | |
jschilling22 | 0:9dfb24edf079 | 11 | AnalogIn tmp36(p18); |
jschilling22 | 0:9dfb24edf079 | 12 | Serial pc(USBTX,USBRX); |
jschilling22 | 0:9dfb24edf079 | 13 | SDFileSystem fs(p5, p6, p7, p8, "fs"); |
jschilling22 | 0:9dfb24edf079 | 14 | Timer t; |
jschilling22 | 0:9dfb24edf079 | 15 | Ticker sampleTime; |
jschilling22 | 0:9dfb24edf079 | 16 | DigitalOut doneLED(LED1); |
jschilling22 | 0:9dfb24edf079 | 17 | bool timeToRead; |
jschilling22 | 0:9dfb24edf079 | 18 | |
jschilling22 | 0:9dfb24edf079 | 19 | void triggerCollection() { |
jschilling22 | 0:9dfb24edf079 | 20 | timeToRead = true; // flag to collect data |
jschilling22 | 0:9dfb24edf079 | 21 | } |
jschilling22 | 0:9dfb24edf079 | 22 | |
jschilling22 | 0:9dfb24edf079 | 23 | int main() { |
jschilling22 | 0:9dfb24edf079 | 24 | |
jschilling22 | 0:9dfb24edf079 | 25 | float voltage, temperature; |
jschilling22 | 0:9dfb24edf079 | 26 | // Mount the filesystem |
jschilling22 | 0:9dfb24edf079 | 27 | bool mountFailure = fs.mount(); |
jschilling22 | 0:9dfb24edf079 | 28 | if (mountFailure != 0) { |
jschilling22 | 0:9dfb24edf079 | 29 | pc.printf("Failed to mount the SD card.\r\n"); |
jschilling22 | 0:9dfb24edf079 | 30 | return -1; // ends program with error status |
jschilling22 | 0:9dfb24edf079 | 31 | } |
jschilling22 | 0:9dfb24edf079 | 32 | |
jschilling22 | 0:9dfb24edf079 | 33 | FILE* fp = fopen("/fs/log.txt","w"); |
jschilling22 | 0:9dfb24edf079 | 34 | if (fp == NULL) { |
jschilling22 | 0:9dfb24edf079 | 35 | pc.printf("Failed to open the file.\r\n"); |
jschilling22 | 0:9dfb24edf079 | 36 | fs.unmount(); |
jschilling22 | 0:9dfb24edf079 | 37 | return -1; |
jschilling22 | 0:9dfb24edf079 | 38 | } |
jschilling22 | 0:9dfb24edf079 | 39 | |
jschilling22 | 0:9dfb24edf079 | 40 | // Write a header row |
jschilling22 | 0:9dfb24edf079 | 41 | fprintf(fp, "Time (s) \t Temperature (deg C)\r\n"); |
jschilling22 | 0:9dfb24edf079 | 42 | |
jschilling22 | 0:9dfb24edf079 | 43 | // Start the timer and ticker |
jschilling22 | 0:9dfb24edf079 | 44 | t.start(); |
jschilling22 | 0:9dfb24edf079 | 45 | sampleTime.attach(&triggerCollection, 0.5); // write data every 0.5 s |
jschilling22 | 0:9dfb24edf079 | 46 | timeToRead = true; // collect data at t = 0 |
jschilling22 | 0:9dfb24edf079 | 47 | |
jschilling22 | 0:9dfb24edf079 | 48 | while (t.read()<20) { |
jschilling22 | 0:9dfb24edf079 | 49 | if (timeToRead) { |
jschilling22 | 0:9dfb24edf079 | 50 | timeToRead = false; // reset data collection flag |
jschilling22 | 0:9dfb24edf079 | 51 | voltage = 3.3*tmp36.read(); |
jschilling22 | 0:9dfb24edf079 | 52 | temperature = (voltage-0.5)/0.01; |
jschilling22 | 0:9dfb24edf079 | 53 | fprintf(fp,"%.2f \t %.2f\r\n", t.read(),temperature); |
jschilling22 | 0:9dfb24edf079 | 54 | } |
jschilling22 | 0:9dfb24edf079 | 55 | } |
jschilling22 | 0:9dfb24edf079 | 56 | |
jschilling22 | 0:9dfb24edf079 | 57 | // Close the file and unmount the file system so the SD card is happy |
jschilling22 | 0:9dfb24edf079 | 58 | fclose(fp); |
jschilling22 | 0:9dfb24edf079 | 59 | fs.unmount(); |
jschilling22 | 0:9dfb24edf079 | 60 | // Turn on LED to let user know it is safe to remove the SD card |
jschilling22 | 0:9dfb24edf079 | 61 | doneLED = 1; |
jschilling22 | 0:9dfb24edf079 | 62 | } |