Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

Revision:
8:25fc7a5cff17
Parent:
7:f7368fed0a2f
Child:
9:3cbb586b65b2
--- a/main.cpp	Tue Apr 10 21:35:08 2018 +0000
+++ b/main.cpp	Thu Apr 12 15:30:53 2018 +0000
@@ -8,6 +8,9 @@
 DigitalOut ledError(LED3);
 DigitalOut powerOn(p16);
 DigitalOut sdMount(p15);
+DigiatlOut ledRedTube(27);
+DigitalOut ledBlueTube(26);
+AnalogIn lightSensorTube(19);
 
 //This is our timer
 Ticker countClock;
@@ -20,6 +23,8 @@
 
 float checkLightSensor(int n);
 
+float checkTubeLightSensor(int n);
+
 //Switches the on states of the LEDs in the sphere
 void ledSwitch();
 
@@ -29,47 +34,52 @@
 FILE *fp = NULL;
  
 int main() {
-    powerOn = 1; 
-    timeClock.start();
-    sdMount = false;
+    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; 
+        
+    int checkTimes = 60; //This determines how many times the led checks the phototransistor to get its averages
     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");
+    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 Light
+        //Blue Lights
         ledSwitch();
-        pc.printf("%.1f, ", timeClock.read());
-        pc.printf("%.4f, ", checkLightSensor(checkTimes));
-        fprintf(fp,"%.1f, ", timeClock.read());
-        fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
+        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));
         
-        wait(1);
-        
-        //Red Light
+        //Red Lights
         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));
+        pc.printf("%.1f\r\n", timeClock.read());
+        pc.printf("%.4f,", checkLightSensor(checkTimes));
+        pc.printf("%.4f,", checkTubeLightSensor(checkTimes));
         
-        wait(1); 
+        fprintf(fp,"%.1f,", timeClock.read());
+        fprintf(fp,"%.4f,", checkLightSensor(checkTimes));
+        fprintf(fp,"%.4f\n\r", checkTubeLightSensor(checkTimes));
+        
+        wait(2); 
     }
 }
     
@@ -83,21 +93,31 @@
     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(){
-    ledError = true;
     sdMount = true;
     fclose(fp);
     fp = fopen("/sd/mydir/sdtest.txt", "a");
     if(fp == NULL) {
+        ledError = true;
         error("Could not open file for write\n");
-        ledError = true;
     }
     pc.printf("\n\rSaved\n\r");
-    ledError = false;
     sdMount = false;
 }
\ No newline at end of file