Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

main.cpp

Committer:
oldmanturtle
Date:
2018-04-12
Revision:
8:25fc7a5cff17
Parent:
7:f7368fed0a2f
Child:
9:3cbb586b65b2

File content as of revision 8:25fc7a5cff17:

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

AnalogIn lightSensor(p20); 
DigitalOut ledRed(p25);
DigitalOut ledBlue(p26);
DigitalOut ledError(LED3);
DigitalOut powerOn(p16);
DigitalOut sdMount(p15);
DigiatlOut ledRedTube(27);
DigitalOut ledBlueTube(26);
AnalogIn lightSensorTube(19);

//This is our timer
Ticker countClock;

ExtendedTimer timeClock;

SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

Serial pc(USBTX,USBRX); 

float checkLightSensor(int n);

float checkTubeLightSensor(int n);

//Switches the on states of the LEDs in the sphere
void ledSwitch();

//This should save the data to the sd card *This isn't working right now*
void save();

FILE *fp = NULL;
 
int main() {
    powerOn = 1;  //Turn on the power light on the module
    timeClock.start(); //Start the clock to take the time
    sdMount = false;//Set the sdMount light to default to off
    
    //This bit happens if the SD card doesn't mount
    if (sd.mount() != 0) {
        pc.printf("Failed to mount the SD card.\r\n");
        sdMount = true;
        return -1;  // ends program with error status
    }
    
    //Open the file and append it
    fp = fopen("/sd/mydir/sdtest.txt", "a");
    if(fp == NULL) {
        sdMount = true;
        ledError = true;
        error("Could not open file for write\n");
        return -1;
    }
        
    int checkTimes = 60; //This determines how many times the led checks the phototransistor to get its averages
    ledRed = true;
    ledBlue = false;
    countClock.attach(&save, 120);
    fprintf(fp,"\r\n\r\n\r\n\r\nTimeBlueDataTaken,BlueLight,BlueLightTube,TimeRedDataTaken,RedLight,RedLightTube\n\r\n\r\n\r");
    while(true) {
        //Blue Lights
        ledSwitch();
        pc.printf("%.1f,", timeClock.read());
        pc.printf("%.4f,", checkLightSensor(checkTimes));
        pc.printf("%.4f,", checkTubeLightSensor(checkTimes));
        fprintf(fp,"%.1f,", timeClock.read());
        fprintf(fp,"%.4f,", checkLightSensor(checkTimes));
        fprintf(fp,"%.4f,", checkTubeLightSensor(checkTimes));
        
        //Red Lights
        ledSwitch();
        pc.printf("%.1f\r\n", timeClock.read());
        pc.printf("%.4f,", checkLightSensor(checkTimes));
        pc.printf("%.4f,", checkTubeLightSensor(checkTimes));
        
        fprintf(fp,"%.1f,", timeClock.read());
        fprintf(fp,"%.4f,", checkLightSensor(checkTimes));
        fprintf(fp,"%.4f\n\r", checkTubeLightSensor(checkTimes));
        
        wait(2); 
    }
}
    
    // Average n readings of the light sensor 
float checkLightSensor(int n){
    float x;
        x = 0; 
    for (int i = 0; i<n; i++) 
        x = x + lightSensor; 
    x = x/n;
    return x;
}

    // Average n readings of the light sensor in the tube
float checkTubeLightSensor(int n){
    float x;
        x = 0; 
    for (int i = 0; i<n; i++) 
        x = x + lightSensorTube; 
    x = x/n;
    return x;
}

void ledSwitch(){
    ledBlue = !ledBlue;
    ledRed = !ledRed;
    ledBlueTube = !ledBlueTube;
    ledRedTube = !ledRedTube;
}

void save(){
    sdMount = true;
    fclose(fp);
    fp = fopen("/sd/mydir/sdtest.txt", "a");
    if(fp == NULL) {
        ledError = true;
        error("Could not open file for write\n");
    }
    pc.printf("\n\rSaved\n\r");
    sdMount = false;
}