Amundson OCE 360 homework 7, write and store temperature and time data to an SD card
Dependencies: SDFileSystem mbed
main.cpp@1:51cb4f1ece04, 2018-11-03 (annotated)
- Committer:
- bamundson
- Date:
- Sat Nov 03 19:13:23 2018 +0000
- Revision:
- 1:51cb4f1ece04
- Parent:
- 0:4145a0dc56d8
Amundson OCE 360 homework 7, write and store temperature and time data to an SD card
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bamundson | 0:4145a0dc56d8 | 1 | // Benjamin Amundson OCE Homework 6 10/23/2018 |
bamundson | 0:4145a0dc56d8 | 2 | // |
bamundson | 0:4145a0dc56d8 | 3 | //last revision 11/1/2018 |
bamundson | 0:4145a0dc56d8 | 4 | |
bamundson | 0:4145a0dc56d8 | 5 | #include "mbed.h" |
bamundson | 0:4145a0dc56d8 | 6 | #include "SDFileSystem.h" |
bamundson | 0:4145a0dc56d8 | 7 | #include "temperatureValues.h" |
bamundson | 0:4145a0dc56d8 | 8 | #include "inputOutput.h" |
bamundson | 0:4145a0dc56d8 | 9 | |
bamundson | 0:4145a0dc56d8 | 10 | int main (){ |
bamundson | 0:4145a0dc56d8 | 11 | FILE *file; |
bamundson | 0:4145a0dc56d8 | 12 | float outputVoltage; |
bamundson | 0:4145a0dc56d8 | 13 | float temperature; |
bamundson | 0:4145a0dc56d8 | 14 | float initialTemperature = ((SOURCE_VOLTAGE_MV-(SOURCE_VOLTAGE_MV-Ain))); |
bamundson | 0:4145a0dc56d8 | 15 | float temperatureChange; |
bamundson | 0:4145a0dc56d8 | 16 | int c; |
bamundson | 0:4145a0dc56d8 | 17 | |
bamundson | 0:4145a0dc56d8 | 18 | |
bamundson | 0:4145a0dc56d8 | 19 | timer.start(); |
bamundson | 0:4145a0dc56d8 | 20 | file = fopen("/sd/temp_data.txt", "w"); |
bamundson | 0:4145a0dc56d8 | 21 | |
bamundson | 0:4145a0dc56d8 | 22 | while (1) { |
bamundson | 0:4145a0dc56d8 | 23 | |
bamundson | 0:4145a0dc56d8 | 24 | if(switchValue == 1) { |
bamundson | 0:4145a0dc56d8 | 25 | file = fopen("/sd/temp_data.txt", "a+"); |
bamundson | 0:4145a0dc56d8 | 26 | if ( file == NULL ) { |
bamundson | 0:4145a0dc56d8 | 27 | error("ERROR: Could not open file for writing!\n\r"); |
bamundson | 0:4145a0dc56d8 | 28 | return -1; |
bamundson | 0:4145a0dc56d8 | 29 | } |
bamundson | 0:4145a0dc56d8 | 30 | pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r"); |
bamundson | 0:4145a0dc56d8 | 31 | |
bamundson | 0:4145a0dc56d8 | 32 | outputVoltage = (SOURCE_VOLTAGE_MV*Ain); |
bamundson | 0:4145a0dc56d8 | 33 | temperature = ((SOURCE_VOLTAGE_MV-(SOURCE_VOLTAGE_MV*Ain)) * MV_TO_VOLTAGE_CONV); |
bamundson | 0:4145a0dc56d8 | 34 | temperatureChange = initialTemperature - temperature; |
bamundson | 0:4145a0dc56d8 | 35 | fprintf(file, "%2.2fs: %3.2f mV %3.2f deg %3.2f degree change from initial C\n\r", timer.read(), outputVoltage, temperature, temperatureChange); |
bamundson | 0:4145a0dc56d8 | 36 | |
bamundson | 0:4145a0dc56d8 | 37 | pc.printf("Output Voltage (mV): %3.2f \r\n", outputVoltage); |
bamundson | 0:4145a0dc56d8 | 38 | pc.printf("Current Temperature degrees C: %3.1f \r\n", temperature); |
bamundson | 0:4145a0dc56d8 | 39 | pc.printf("Current Time since powered on: %2.2f \r\n", timer.read()); |
bamundson | 0:4145a0dc56d8 | 40 | fclose(file); |
bamundson | 0:4145a0dc56d8 | 41 | wait(2.0); |
bamundson | 0:4145a0dc56d8 | 42 | |
bamundson | 0:4145a0dc56d8 | 43 | |
bamundson | 0:4145a0dc56d8 | 44 | } |
bamundson | 0:4145a0dc56d8 | 45 | |
bamundson | 0:4145a0dc56d8 | 46 | else { |
bamundson | 0:4145a0dc56d8 | 47 | if ( file == NULL ) { |
bamundson | 0:4145a0dc56d8 | 48 | error("ERROR: Could not open file for writing!\n\r"); |
bamundson | 0:4145a0dc56d8 | 49 | return -1; |
bamundson | 0:4145a0dc56d8 | 50 | } |
bamundson | 0:4145a0dc56d8 | 51 | |
bamundson | 0:4145a0dc56d8 | 52 | fclose(file); |
bamundson | 0:4145a0dc56d8 | 53 | file = fopen("/sd/temp_data.txt", "r"); |
bamundson | 0:4145a0dc56d8 | 54 | pc.printf("Temperature data:\n\r"); |
bamundson | 0:4145a0dc56d8 | 55 | while(1) { |
bamundson | 0:4145a0dc56d8 | 56 | c = fgetc(file); |
bamundson | 0:4145a0dc56d8 | 57 | if ( c == EOF ) { |
bamundson | 0:4145a0dc56d8 | 58 | break; |
bamundson | 0:4145a0dc56d8 | 59 | } |
bamundson | 0:4145a0dc56d8 | 60 | pc.putc(c); |
bamundson | 0:4145a0dc56d8 | 61 | |
bamundson | 0:4145a0dc56d8 | 62 | } |
bamundson | 0:4145a0dc56d8 | 63 | |
bamundson | 0:4145a0dc56d8 | 64 | // Close the file and finish |
bamundson | 0:4145a0dc56d8 | 65 | fclose(file); |
bamundson | 0:4145a0dc56d8 | 66 | pc.printf("Done! Safe to remove SD card\n\r"); |
bamundson | 0:4145a0dc56d8 | 67 | wait(2); |
bamundson | 0:4145a0dc56d8 | 68 | |
bamundson | 0:4145a0dc56d8 | 69 | |
bamundson | 0:4145a0dc56d8 | 70 | } |
bamundson | 0:4145a0dc56d8 | 71 | |
bamundson | 0:4145a0dc56d8 | 72 | |
bamundson | 0:4145a0dc56d8 | 73 | |
bamundson | 0:4145a0dc56d8 | 74 | |
bamundson | 0:4145a0dc56d8 | 75 | } |
bamundson | 0:4145a0dc56d8 | 76 | } |