Implement a SD card into HW6
Dependencies: SDFileSystem mbed
Fork of shomberg_hw_6 by
main.cpp@19:1f4dd59bbe6b, 2018-10-31 (annotated)
- Committer:
- rshomberg
- Date:
- Wed Oct 31 23:32:01 2018 +0000
- Revision:
- 19:1f4dd59bbe6b
- Parent:
- 18:699b41309be7
- Child:
- 20:fc1690076f87
Untested code that compiles
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rshomberg | 0:82635173a413 | 1 | /** |
rshomberg | 19:1f4dd59bbe6b | 2 | Temperature Sensor and Logging for hw7 |
rshomberg | 0:82635173a413 | 3 | main.cpp |
rshomberg | 0:82635173a413 | 4 | |
rshomberg | 6:8cfa0216554f | 5 | Purpose: Read signal from TMP36 connected to pin20 |
rshomberg | 18:699b41309be7 | 6 | Toggle switch connected to pin07 activates SD logging |
rshomberg | 18:699b41309be7 | 7 | Log: Time Elasped, Current Voltage, Current Temp, Delta Temp |
rshomberg | 18:699b41309be7 | 8 | Finish log -> save file and send to USB serial port |
rshomberg | 18:699b41309be7 | 9 | |
rshomberg | 8:d6560caeda9a | 10 | @author Russell Shomberg |
rshomberg | 18:699b41309be7 | 11 | @created 2018-10-31 |
rshomberg | 18:699b41309be7 | 12 | @revised 2018-10-31 |
rshomberg | 8:d6560caeda9a | 13 | @version 0.0 |
rshomberg | 18:699b41309be7 | 14 | |
rshomberg | 18:699b41309be7 | 15 | Issues: |
rshomberg | 18:699b41309be7 | 16 | |
rshomberg | 18:699b41309be7 | 17 | */ |
rshomberg | 0:82635173a413 | 18 | |
rshomberg | 0:82635173a413 | 19 | // INCLUDES |
rshomberg | 0:82635173a413 | 20 | #include "mbed.h" |
rshomberg | 9:da0b72918880 | 21 | #include "OCE360Input.h" |
rshomberg | 18:699b41309be7 | 22 | #include "SDFileSystem.h" |
rshomberg | 0:82635173a413 | 23 | |
rshomberg | 0:82635173a413 | 24 | // OUTPUTS |
rshomberg | 2:312a0a9c4485 | 25 | Serial pc(USBTX, USBRX); // for debugging |
rshomberg | 6:8cfa0216554f | 26 | |
rshomberg | 0:82635173a413 | 27 | // VARIABLES |
rshomberg | 6:8cfa0216554f | 28 | int outputT= 0; |
rshomberg | 18:699b41309be7 | 29 | float starting_voltage; |
rshomberg | 18:699b41309be7 | 30 | float starting_temp; |
rshomberg | 18:699b41309be7 | 31 | |
rshomberg | 18:699b41309be7 | 32 | float current_time; |
rshomberg | 18:699b41309be7 | 33 | float current_voltage; |
rshomberg | 18:699b41309be7 | 34 | float current_temp; |
rshomberg | 19:1f4dd59bbe6b | 35 | int file_counter=0; |
rshomberg | 19:1f4dd59bbe6b | 36 | char file_name [20]; |
rshomberg | 19:1f4dd59bbe6b | 37 | Timer t; |
rshomberg | 19:1f4dd59bbe6b | 38 | FILE *fp; |
rshomberg | 19:1f4dd59bbe6b | 39 | |
rshomberg | 19:1f4dd59bbe6b | 40 | // Initialize SDFileSystem |
rshomberg | 19:1f4dd59bbe6b | 41 | SDFileSystem sd(p12,p13,p14,p15,"sd"); |
rshomberg | 18:699b41309be7 | 42 | |
rshomberg | 6:8cfa0216554f | 43 | |
rshomberg | 0:82635173a413 | 44 | |
rshomberg | 18:699b41309be7 | 45 | int main() |
rshomberg | 18:699b41309be7 | 46 | { |
rshomberg | 19:1f4dd59bbe6b | 47 | //Initialize Timer |
rshomberg | 19:1f4dd59bbe6b | 48 | t.start(); |
rshomberg | 19:1f4dd59bbe6b | 49 | |
rshomberg | 19:1f4dd59bbe6b | 50 | // Mount the sd and |
rshomberg | 19:1f4dd59bbe6b | 51 | printf("Mounting SD Card\n\r"); |
rshomberg | 19:1f4dd59bbe6b | 52 | sd.mount(); |
rshomberg | 18:699b41309be7 | 53 | |
rshomberg | 0:82635173a413 | 54 | while(1) { |
rshomberg | 18:699b41309be7 | 55 | while(read_switch()) { // skip everything if switch is off |
rshomberg | 18:699b41309be7 | 56 | |
rshomberg | 18:699b41309be7 | 57 | // Initialize a file |
rshomberg | 19:1f4dd59bbe6b | 58 | printf("Opening a new file: temp_log%d.csv\n\r",file_counter); |
rshomberg | 19:1f4dd59bbe6b | 59 | sprintf(file_name,"/sd/temp_log%d.csv",file_counter); |
rshomberg | 19:1f4dd59bbe6b | 60 | fp = fopen(file_name,"w"); |
rshomberg | 18:699b41309be7 | 61 | |
rshomberg | 19:1f4dd59bbe6b | 62 | if (fp!=NULL) { // Don't procede if there is an issue |
rshomberg | 18:699b41309be7 | 63 | |
rshomberg | 19:1f4dd59bbe6b | 64 | // Write header to file |
rshomberg | 19:1f4dd59bbe6b | 65 | fprintf(fp,"Time (s), Voltage (mv), Temperature (C), Delta T (C)\n\r"); |
rshomberg | 19:1f4dd59bbe6b | 66 | |
rshomberg | 19:1f4dd59bbe6b | 67 | // Take initial readings |
rshomberg | 19:1f4dd59bbe6b | 68 | starting_voltage = read_sensor(); |
rshomberg | 19:1f4dd59bbe6b | 69 | starting_temp = convert_mV_to_temp(starting_voltage); |
rshomberg | 18:699b41309be7 | 70 | |
rshomberg | 19:1f4dd59bbe6b | 71 | // start a loop that logging |
rshomberg | 19:1f4dd59bbe6b | 72 | // exiting this loop will initialize data save procedures |
rshomberg | 19:1f4dd59bbe6b | 73 | while(read_switch()) { // Main loop for logging |
rshomberg | 19:1f4dd59bbe6b | 74 | printf("Logging data....\n\r"); |
rshomberg | 19:1f4dd59bbe6b | 75 | // Get data for logging |
rshomberg | 19:1f4dd59bbe6b | 76 | current_time = t.read(); |
rshomberg | 19:1f4dd59bbe6b | 77 | current_voltage = read_sensor(); |
rshomberg | 19:1f4dd59bbe6b | 78 | current_temp = convert_mV_to_temp(current_voltage); |
rshomberg | 18:699b41309be7 | 79 | |
rshomberg | 19:1f4dd59bbe6b | 80 | fprintf(fp,"%1.2f, %1.2f, %1.2f, %1.2f \n\r", current_time, current_voltage, current_temp, current_temp-starting_temp); |
rshomberg | 19:1f4dd59bbe6b | 81 | |
rshomberg | 19:1f4dd59bbe6b | 82 | wait(1); |
rshomberg | 19:1f4dd59bbe6b | 83 | } |
rshomberg | 19:1f4dd59bbe6b | 84 | } else { // runs if there was a issue opening the file |
rshomberg | 19:1f4dd59bbe6b | 85 | printf("Issue with opening file\n\r"); |
rshomberg | 18:699b41309be7 | 86 | } |
rshomberg | 19:1f4dd59bbe6b | 87 | |
rshomberg | 18:699b41309be7 | 88 | // This code runs when the switch is toggled off |
rshomberg | 18:699b41309be7 | 89 | // Close out file |
rshomberg | 19:1f4dd59bbe6b | 90 | printf("Logging complete! \n\r Saving file \n\r"); |
rshomberg | 19:1f4dd59bbe6b | 91 | fclose(fp); |
rshomberg | 19:1f4dd59bbe6b | 92 | |
rshomberg | 18:699b41309be7 | 93 | // Print to USB serial |
rshomberg | 19:1f4dd59bbe6b | 94 | printf("Uploading data to USB"); |
rshomberg | 19:1f4dd59bbe6b | 95 | // |
rshomberg | 19:1f4dd59bbe6b | 96 | fp = fopen(file_name,"r"); |
rshomberg | 19:1f4dd59bbe6b | 97 | if (fp!=NULL) { // Don't procede if there is an issue |
rshomberg | 19:1f4dd59bbe6b | 98 | printf("I don't know how to do this"); |
rshomberg | 19:1f4dd59bbe6b | 99 | } else { // runs if there was a issue opening the file |
rshomberg | 19:1f4dd59bbe6b | 100 | printf("Issue with opening file\n\r"); |
rshomberg | 19:1f4dd59bbe6b | 101 | fclose(fp); |
rshomberg | 19:1f4dd59bbe6b | 102 | file_counter++; |
rshomberg | 19:1f4dd59bbe6b | 103 | } |
rshomberg | 6:8cfa0216554f | 104 | } |
rshomberg | 18:699b41309be7 | 105 | // Re-enter main while loop |
rshomberg | 18:699b41309be7 | 106 | //continuously check for switch to turn on again |
rshomberg | 0:82635173a413 | 107 | } |
rshomberg | 18:699b41309be7 | 108 | } |