Rough Draft, still some debugging to be done. Considering getting rubber ducky

Dependencies:   mbed SDFileSystem DS1820 ExtendedTimer

main.cpp

Committer:
hzelayasolano22
Date:
2019-04-11
Revision:
0:2fc99d74f745
Child:
1:c283bc6f3938

File content as of revision 0:2fc99d74f745:

//////////////////////////////
//Main Code for Themal Wake //
//Jonathan Zelaya           //
//April 11, 2019            //
//////////////////////////////

#define MULTIPLE_PROBES
#define ARM1_DATA_PIN p25
#define ARM2_DATA_PIN p19
#define TAIL_DATA_PIN p23
#define MAX_PROBES 25

#include "mbed.h"
#include "DS1820.h"
#include "ExtendedTimer.h"
#include "SDFileSystem.h"

DS1820* probe1[MAX_PROBES];
DS1820* probe2[MAX_PROBES];
DS1820* probeTail[MAX_PROBES];
SDFileSystem fs(p5, p6, p7, p8, "fs");
Timer t;
Ticker sampleTime;
Ticker saveTime;
DigitalOut led1(LED1);
DigitalOut led3(p12);
DigitalOut ledOn(p15);
DigitalOut ledError(p16);

bool timeToRead;
bool timeToSave;
void triggerSave();
void triggerCollection();



int main(){
    int num_arm1 = 0;
    int num_arm2 = 0;
    int num_tail = 0;
    //mount filesystem
    bool mountFailure = fs.mount();
    if(mountFailure != 0){
        ledError = 1;
        return -1;//end program with error status
        }
        else 
            ledOn = 1;
            
    unsigned long long fullName; //DS1820 is 64 bits( a long long)
    FILE* out1 = fopen("/fs/arm1.txt","w");
    FILE* out2 = fopen("/fs/arm2.txt","w");
    FILE* out3 = fopen("/fs/tail.txt","w");
    
    //arm 1
    while(DS1820::unassignedProbe(ARM1_DATA_PIN)){
        probe1[num_arm1] = new DS1820(ARM1_DATA_PIN);
        num_arm1++;
        if(num_arm1 == MAX_PROBES){
            break;
            }
      
    //arm2      
    while(DS1820::unassignedProbe(ARM2_DATA_PIN)){
        probe1[num_arm2] = new DS1820(ARM2_DATA_PIN);
        num_arm2++;
        if(num_arm2 == MAX_PROBES){
            break;
            }
    
    //arm3
    while(DS1820::unassignedProbe(TAIL_DATA_PIN)){
        probe1[num_tail] = new DS1820(TAIL_DATA_PIN);
        num_tail++;
        if(num_tail == MAX_PROBES){
            break;
            }
    for(int i = 0; i < num_arm1; i++){
        fullName = probe1[1]->whoAmI();
        //print 64-bit ID as a hexadecimal number
        fprintf(out1,"%llX\t", fullName);
    
    for(int i = 0; i < num_arm2; i++){
        fullName = probe2[1]->whoAmI();
        //print 64-bit ID as a hexadecimal number
        fprintf(out2,"%llX\t", fullName);
        
    for(int i = 0; i < num_tail; i++){
        fullName = probeTail[1]->whoAmI();
        //print 64-bit ID as a hexadecimal number
        fprintf(out3,"%llX\t", fullName);

    fprintf(out1,"\r\n");
    fprintf(out2,"\r\n");
    fprintf(out3,"\r\n");
    
    //Writing headers for each file!
    fprintf(out1, "Time (s) \t Tempurature (deg C)\r\n");
    fprintf(out2, "Time (s) \t Tempurature (deg C)\r\n");
    fprintf(out3, "Time (s) \t Tempurature (deg C)\r\n");
    fprintf(out1, "Found %d device(s)\r\n", num_arm1);//lets user know how many sensors were read during the actual flight
    fprintf(out2, "Found %d device(s)\r\n", num_arm2);
    fprintf(out3, "Found %d device(s)\r\n", num_tail);
    for(int i = 0; i < num_arm1; i++){//reads out the ID numbers to each file and numbers them
        fullName = probe1[1]->whoAmI();
        fprintf(out1, "\tID%d = \r\n", i, fullName);
        }
    for(int i = 0; i < num_arm2; i++){
        fullName = probe2[1]->whoAmI();
        fprintf(out2, "\tID%d = \r\n", i, fullName);
        }
    for(int i = 0; i < num_tail; i++){
        fullName = probeTail[1]->whoAmI();
        fprintf(out3, "\tID%d = \r\n", i, fullName);
        }
    
        
//timers
    t.start();
    sampleTime.attach(&triggerCollection, 10);//receive data every 10 seconds
    timeToRead = true;
    
    timeToSave = false;
    saveTime.attach(&triggerSave, 30);
    
    while(t.read() < (60*60*4)){ //4 hours of data
        if(timeToRead == true){
            timeToRead = false;//reset the clock
            probe1[0]-> convertTemperature(true, DS1820::all_devices);
            probe2[0]-> convertTemperature(true, DS1820::all_devices);
            probeTail[0]-> convertTemperature(true, DS1820::all_devices);
        //print data from arm 1
            fprintf(out1, "%3.1f/t", t.read());
            //get temp from sensors
            for(int i = 0; i < num_arm1; i++){
                fprintf(out1, "Sensor %d = &3.1f/t", i, probe1[i]->temperature());
                fprintf(out1, "r/n");
                }

            for(int i = 0; i < num_arm2; i++){
                fprintf(out2, "Sensor %d = &3.1f/t", i, probe2[i]->temperature());
                fprintf(out2, "r/n");
                }
                
            for(int i = 0; i < num_tail; i++){
                fprintf(out3, "Sensor %d = &3.1f/t", i, probeTail[i]->temperature());
                fprintf(out3, "r/n");
                
                }

    
        if(timeToSave){
            ledError = 1;
            timeToSave = false;
            fclose(out1);
            fclose(out2);
            fclose(out3);
            out1 = fopen("/fs/arm1.txt","a");
            out2 = fopen("/fs/arm2.txt","a");
            out3 = fopen("/fs/tail.txt","a");
            ledError = 0;
            }
        }
    }
    
//close the file and unmount the file system 
    fclose(out1);
    fclose(out2);
    fclose(out3);
    fs.unmount();
    led1 = 1;//turn on LED to let user know its safe to remove SD card
    }

void triggerSave(){
    timeToSave = true;
    }
void triggerCollection(){
    timeToRead = true;
    }