Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

main.cpp

Committer:
oldmanturtle
Date:
2018-03-10
Revision:
4:327441ad8cf6
Parent:
3:9d9628bd3514
Child:
5:603c549bfefa

File content as of revision 4:327441ad8cf6:

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

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

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

void ledSwitch();

void save();

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