Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

main.cpp

Committer:
oldmanturtle
Date:
2018-04-10
Revision:
7:f7368fed0a2f
Parent:
6:892ecb5fcfb9
Child:
8:25fc7a5cff17

File content as of revision 7:f7368fed0a2f:

#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);

//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);

//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; 
    timeClock.start();
    sdMount = false;
    if (sd.mount() != 0) {
        pc.printf("Failed to mount the SD card.\r\n");
        sdMount = true;
        return -1;  // ends program with error status
    }
    
    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; 
    ledRed = true;
    ledBlue = false;
    countClock.attach(&save, 10);
    fprintf(fp,"\r\n\r\n\r\n\r\nBlue Light, Time Blue Data Taken, Red Light, Time Red Data Taken\n\r\n\r\n\r");
    while(true) {
        //Blue Light
        ledSwitch();
        pc.printf("%.1f, ", timeClock.read());
        pc.printf("%.4f, ", checkLightSensor(checkTimes));
        fprintf(fp,"%.1f, ", timeClock.read());
        fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
        
        wait(1);
        
        //Red Light
        ledSwitch();
        pc.printf("%.1f\r\n ", timeClock.read());
        pc.printf("%.4f, ", checkLightSensor(checkTimes));
        fprintf(fp,"%.1f\r\n", timeClock.read());
        fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
        
        wait(1); 
    }
}
    
    // 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;
}

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

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