Amundson OCE 360 homework 7, write and store temperature and time data to an SD card

Dependencies:   SDFileSystem mbed

main.cpp

Committer:
bamundson
Date:
2018-11-03
Revision:
1:51cb4f1ece04
Parent:
0:4145a0dc56d8

File content as of revision 1:51cb4f1ece04:

// Benjamin Amundson OCE Homework 6 10/23/2018
//
//last revision 11/1/2018

#include "mbed.h"
#include "SDFileSystem.h"
#include "temperatureValues.h"
#include "inputOutput.h"

int main (){
    FILE *file;
    float outputVoltage;
    float temperature;
    float initialTemperature = ((SOURCE_VOLTAGE_MV-(SOURCE_VOLTAGE_MV-Ain)));
    float temperatureChange;
    int c;


    timer.start();
    file = fopen("/sd/temp_data.txt", "w");

    while (1) {

        if(switchValue == 1) {
            file = fopen("/sd/temp_data.txt", "a+");
            if ( file == NULL ) {
                error("ERROR: Could not open file for writing!\n\r");
                return -1;
            }
            pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r");

            outputVoltage = (SOURCE_VOLTAGE_MV*Ain);
            temperature = ((SOURCE_VOLTAGE_MV-(SOURCE_VOLTAGE_MV*Ain)) * MV_TO_VOLTAGE_CONV);
            temperatureChange = initialTemperature - temperature;
            fprintf(file, "%2.2fs: %3.2f mV %3.2f deg %3.2f degree change from initial C\n\r", timer.read(), outputVoltage, temperature, temperatureChange);

            pc.printf("Output Voltage (mV): %3.2f \r\n", outputVoltage);
            pc.printf("Current Temperature degrees C: %3.1f \r\n", temperature);
            pc.printf("Current Time since powered on: %2.2f \r\n", timer.read());
            fclose(file);
            wait(2.0);


        }

        else {
            if ( file == NULL ) {
                error("ERROR: Could not open file for writing!\n\r");
                return -1;
            }

            fclose(file);
            file = fopen("/sd/temp_data.txt", "r");
            pc.printf("Temperature data:\n\r");
            while(1) {
                c = fgetc(file);
                if ( c == EOF ) {
                    break;
                }
                pc.putc(c);

            }

            // Close the file and finish
            fclose(file);
            pc.printf("Done! Safe to remove SD card\n\r");
            wait(2);


        }




    }
}