chemical dosing section of the rig

Dependencies:   ConfigFile mbed

Fork of mbed_plant_rig by Michael Steel

main.cpp

Committer:
Demonthorn
Date:
2017-07-29
Revision:
1:d022bdae586b
Parent:
0:790a9e0285d6

File content as of revision 1:d022bdae586b:

////////////////////////////////////////////////////////////////
//  Plant dosing Rig V1.0.06                                  //
//  Michael Steel                                             //
////////////////////////////////////////////////////////////////

// Libraries
#include "mbed.h"
#include "ConfigFile.h"
// Define I/O and constants

Serial pc(USBTX, USBRX);
DigitalOut DOSE_PUMP(p5);
DigitalOut BLED(p7);
DigitalOut GLED(p8);
DigitalOut RLED(p9);

Timeout dosing;
LocalFileSystem local("local");
ConfigFile cfg;
float DOSEI;
float DOSED;
float LOG_INDEX; /*while not necesary with a single test subject
this will allow for the proper selection of output message in the full version*/
int TCHECK;
int YEAR;
char buffer[21];

void DOSE(void)
{
    DOSE_PUMP = 1;
    GLED = 1;
    time_t seconds = time(NULL);
    strftime(buffer, 21,"%X, %x\n", localtime(&seconds));
    wait (DOSED);
    DOSE_PUMP = 0;
    GLED = 0;
    LOG_INDEX = 1;
}

int main()
{

    // Determins Input file variables
    char *key1 = "DOSEI";
    char *key2 = "DOSED";
    char *key5 = "YEAR";
    char value[BUFSIZ];

    // Read the configuration file from the mbed
    cfg.read("/local/input.cfg");
    if (cfg.getValue(key5, &value[0], sizeof(value))) {
        YEAR = atoi(value) ;
    }
    pc.printf("\r\n Plant Research Rig V1.0 \r\n Resart occured loading variables \r\n");
    // Check correct time and get the current time from the terminal if needed
    struct tm t;
    time_t seconds = time(NULL);
    strftime(buffer, 21, "%Y\n", localtime(&seconds));
    TCHECK = atoi(buffer) ;
    if (TCHECK != YEAR ) {
        printf("Enter current date and time:\n");
        printf("YYYY MM DD HH MM SS[enter]\n");
        scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
              , &t.tm_hour, &t.tm_min, &t.tm_sec);

        // adjust for tm structure required values
        t.tm_year = t.tm_year - 1900;
        t.tm_mon = t.tm_mon - 1;

        // set the time
        set_time(mktime(&t));

        // display the time
        time_t seconds = time(NULL);

    }
    wait(1);
    if (cfg.getValue(key1, &value[0], sizeof(value))) {
        pc.printf("'%s'='%s'\r\n", key1, value);
        DOSEI = atof(value) ;
    } else {
        pc.printf("Failure to get a configuration. Please check config file");
    }
    if (cfg.getValue(key2, &value[0], sizeof(value))) {
        pc.printf("'%s'='%s'\r\n", key2, value);
        DOSED = atof(value) ;
    }
    Ticker dosing;
    dosing.attach(&DOSE, DOSEI); // setup dosing to call DOSE after DOSEI "dose interval" seconds

    pc.printf("Begin logging \r\n");
    while(1) {

        if (LOG_INDEX == 1) {
            FILE *fp = fopen("/local/out.txt", "a");  // Open "out.txt" on the local file system for writing
            fprintf(fp, "\r\n %f s dose given at %s",DOSED ,buffer);
            fclose(fp);
            pc.printf("\r\n %f s dose given to test subject 1 at %s",DOSED ,buffer);
            LOG_INDEX = 0;
        }
        /*if (LOG_INDEX == 2){
        FILE *fp = fopen("/local/out.txt", "a");  // Open "out.txt" on the local file system for writing
        fprintf(fp, "\r\n T: %f s dose given at %s,",DOSEI,buffer);
        fclose(fp);
        pc.printf("\r\n T: %f s dose given to test subject 2 at %s,",DOSEI,buffer);
        LOG_INDEX = 0;
        }
        // this is then repeated for each test subject with sequential numbers */

        wait(1);


    }
}