Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

main.cpp

Committer:
oldmanturtle
Date:
2018-04-05
Revision:
5:603c549bfefa
Parent:
4:327441ad8cf6
Child:
6:892ecb5fcfb9

File content as of revision 5:603c549bfefa:

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

AnalogIn lightSensor(p20); 
DigitalOut ledRed(p25);
DigitalOut ledBlue(p26);
DigitalOut ledError(LED3);

//This is our timer
Ticker countClock;

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() { 
    fp = fopen("/sd/mydir/sdtest.txt", "a");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    int checkTimes = 10; 
    ledRed = true;
    ledBlue = false;
    
   
    if(fp == NULL) {
        error("Could not open file for write\n");
        ledError = true;
    }
    countClock.attach(&save, 10);
    fprintf(fp,"\n\r\n\r\n\rBlue Light, Red Light\n\r\n\r\n\r");
    while(true) {
        //Blue Light
        ledSwitch();
        pc.printf("%.4f, ", checkLightSensor(checkTimes));
        fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
        wait(0.2);
        
        //Red Light
        ledSwitch();
        pc.printf("%.4f\r\n", checkLightSensor(checkTimes));
        fprintf(fp,"%.4f\r\n", checkLightSensor(checkTimes));
        wait(0.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;
}

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

void save(){
    ledError = 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;
}