Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

Committer:
oldmanturtle
Date:
Thu Apr 12 15:46:31 2018 +0000
Revision:
9:3cbb586b65b2
Parent:
8:25fc7a5cff17
Child:
10:07df00712f4e
1.4; Fixed Tube;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
_laurentaylorrr 0:46b2c955924b 1 #include "mbed.h"
oldmanturtle 6:892ecb5fcfb9 2 #include "ExtendedTimer.h"
oldmanturtle 4:327441ad8cf6 3 #include "SDFileSystem.h"
oldmanturtle 4:327441ad8cf6 4
_laurentaylorrr 0:46b2c955924b 5 AnalogIn lightSensor(p20);
_laurentaylorrr 1:279fcab0c394 6 DigitalOut ledRed(p25);
oldmanturtle 2:c76b070c2a55 7 DigitalOut ledBlue(p26);
oldmanturtle 4:327441ad8cf6 8 DigitalOut ledError(LED3);
oldmanturtle 6:892ecb5fcfb9 9 DigitalOut powerOn(p16);
oldmanturtle 6:892ecb5fcfb9 10 DigitalOut sdMount(p15);
oldmanturtle 9:3cbb586b65b2 11 DigitalOut ledRedTube(p27);
oldmanturtle 9:3cbb586b65b2 12 DigitalOut ledBlueTube(p26);
oldmanturtle 9:3cbb586b65b2 13 AnalogIn lightSensorTube(p19);
oldmanturtle 4:327441ad8cf6 14
oldmanturtle 5:603c549bfefa 15 //This is our timer
oldmanturtle 4:327441ad8cf6 16 Ticker countClock;
oldmanturtle 5:603c549bfefa 17
oldmanturtle 6:892ecb5fcfb9 18 ExtendedTimer timeClock;
oldmanturtle 6:892ecb5fcfb9 19
oldmanturtle 4:327441ad8cf6 20 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
oldmanturtle 2:c76b070c2a55 21
oldmanturtle 3:9d9628bd3514 22 Serial pc(USBTX,USBRX);
_laurentaylorrr 1:279fcab0c394 23
oldmanturtle 3:9d9628bd3514 24 float checkLightSensor(int n);
oldmanturtle 3:9d9628bd3514 25
oldmanturtle 8:25fc7a5cff17 26 float checkTubeLightSensor(int n);
oldmanturtle 8:25fc7a5cff17 27
oldmanturtle 5:603c549bfefa 28 //Switches the on states of the LEDs in the sphere
oldmanturtle 3:9d9628bd3514 29 void ledSwitch();
oldmanturtle 3:9d9628bd3514 30
oldmanturtle 5:603c549bfefa 31 //This should save the data to the sd card *This isn't working right now*
oldmanturtle 4:327441ad8cf6 32 void save();
oldmanturtle 4:327441ad8cf6 33
oldmanturtle 5:603c549bfefa 34 FILE *fp = NULL;
oldmanturtle 4:327441ad8cf6 35
oldmanturtle 6:892ecb5fcfb9 36 int main() {
oldmanturtle 8:25fc7a5cff17 37 powerOn = 1; //Turn on the power light on the module
oldmanturtle 8:25fc7a5cff17 38 timeClock.start(); //Start the clock to take the time
oldmanturtle 8:25fc7a5cff17 39 sdMount = false;//Set the sdMount light to default to off
oldmanturtle 8:25fc7a5cff17 40
oldmanturtle 8:25fc7a5cff17 41 //This bit happens if the SD card doesn't mount
oldmanturtle 6:892ecb5fcfb9 42 if (sd.mount() != 0) {
oldmanturtle 6:892ecb5fcfb9 43 pc.printf("Failed to mount the SD card.\r\n");
oldmanturtle 6:892ecb5fcfb9 44 sdMount = true;
oldmanturtle 6:892ecb5fcfb9 45 return -1; // ends program with error status
oldmanturtle 6:892ecb5fcfb9 46 }
oldmanturtle 6:892ecb5fcfb9 47
oldmanturtle 8:25fc7a5cff17 48 //Open the file and append it
oldmanturtle 5:603c549bfefa 49 fp = fopen("/sd/mydir/sdtest.txt", "a");
oldmanturtle 5:603c549bfefa 50 if(fp == NULL) {
oldmanturtle 6:892ecb5fcfb9 51 sdMount = true;
oldmanturtle 6:892ecb5fcfb9 52 ledError = true;
oldmanturtle 5:603c549bfefa 53 error("Could not open file for write\n");
oldmanturtle 6:892ecb5fcfb9 54 return -1;
oldmanturtle 5:603c549bfefa 55 }
oldmanturtle 8:25fc7a5cff17 56
oldmanturtle 8:25fc7a5cff17 57 int checkTimes = 60; //This determines how many times the led checks the phototransistor to get its averages
oldmanturtle 3:9d9628bd3514 58 ledRed = true;
oldmanturtle 3:9d9628bd3514 59 ledBlue = false;
oldmanturtle 8:25fc7a5cff17 60 countClock.attach(&save, 120);
oldmanturtle 8:25fc7a5cff17 61 fprintf(fp,"\r\n\r\n\r\n\r\nTimeBlueDataTaken,BlueLight,BlueLightTube,TimeRedDataTaken,RedLight,RedLightTube\n\r\n\r\n\r");
oldmanturtle 4:327441ad8cf6 62 while(true) {
oldmanturtle 8:25fc7a5cff17 63 //Blue Lights
oldmanturtle 3:9d9628bd3514 64 ledSwitch();
oldmanturtle 8:25fc7a5cff17 65 pc.printf("%.1f,", timeClock.read());
oldmanturtle 8:25fc7a5cff17 66 pc.printf("%.4f,", checkLightSensor(checkTimes));
oldmanturtle 8:25fc7a5cff17 67 pc.printf("%.4f,", checkTubeLightSensor(checkTimes));
oldmanturtle 8:25fc7a5cff17 68 fprintf(fp,"%.1f,", timeClock.read());
oldmanturtle 8:25fc7a5cff17 69 fprintf(fp,"%.4f,", checkLightSensor(checkTimes));
oldmanturtle 8:25fc7a5cff17 70 fprintf(fp,"%.4f,", checkTubeLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 71
oldmanturtle 9:3cbb586b65b2 72 wait(1);
oldmanturtle 9:3cbb586b65b2 73
oldmanturtle 8:25fc7a5cff17 74 //Red Lights
oldmanturtle 3:9d9628bd3514 75 ledSwitch();
oldmanturtle 9:3cbb586b65b2 76 pc.printf("%.1f,", timeClock.read());
oldmanturtle 8:25fc7a5cff17 77 pc.printf("%.4f,", checkLightSensor(checkTimes));
oldmanturtle 9:3cbb586b65b2 78 pc.printf("%.4f\r\n", checkTubeLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 79
oldmanturtle 8:25fc7a5cff17 80 fprintf(fp,"%.1f,", timeClock.read());
oldmanturtle 8:25fc7a5cff17 81 fprintf(fp,"%.4f,", checkLightSensor(checkTimes));
oldmanturtle 8:25fc7a5cff17 82 fprintf(fp,"%.4f\n\r", checkTubeLightSensor(checkTimes));
oldmanturtle 8:25fc7a5cff17 83
oldmanturtle 9:3cbb586b65b2 84 wait(1);
oldmanturtle 4:327441ad8cf6 85 }
oldmanturtle 3:9d9628bd3514 86 }
oldmanturtle 3:9d9628bd3514 87
oldmanturtle 3:9d9628bd3514 88 // Average n readings of the light sensor
oldmanturtle 3:9d9628bd3514 89 float checkLightSensor(int n){
oldmanturtle 3:9d9628bd3514 90 float x;
oldmanturtle 3:9d9628bd3514 91 x = 0;
oldmanturtle 3:9d9628bd3514 92 for (int i = 0; i<n; i++)
oldmanturtle 3:9d9628bd3514 93 x = x + lightSensor;
oldmanturtle 3:9d9628bd3514 94 x = x/n;
oldmanturtle 3:9d9628bd3514 95 return x;
oldmanturtle 3:9d9628bd3514 96 }
oldmanturtle 3:9d9628bd3514 97
oldmanturtle 8:25fc7a5cff17 98 // Average n readings of the light sensor in the tube
oldmanturtle 8:25fc7a5cff17 99 float checkTubeLightSensor(int n){
oldmanturtle 8:25fc7a5cff17 100 float x;
oldmanturtle 8:25fc7a5cff17 101 x = 0;
oldmanturtle 8:25fc7a5cff17 102 for (int i = 0; i<n; i++)
oldmanturtle 8:25fc7a5cff17 103 x = x + lightSensorTube;
oldmanturtle 8:25fc7a5cff17 104 x = x/n;
oldmanturtle 8:25fc7a5cff17 105 return x;
oldmanturtle 8:25fc7a5cff17 106 }
oldmanturtle 8:25fc7a5cff17 107
oldmanturtle 3:9d9628bd3514 108 void ledSwitch(){
oldmanturtle 3:9d9628bd3514 109 ledBlue = !ledBlue;
oldmanturtle 3:9d9628bd3514 110 ledRed = !ledRed;
oldmanturtle 8:25fc7a5cff17 111 ledBlueTube = !ledBlueTube;
oldmanturtle 8:25fc7a5cff17 112 ledRedTube = !ledRedTube;
oldmanturtle 4:327441ad8cf6 113 }
oldmanturtle 4:327441ad8cf6 114
oldmanturtle 4:327441ad8cf6 115 void save(){
oldmanturtle 6:892ecb5fcfb9 116 sdMount = true;
oldmanturtle 4:327441ad8cf6 117 fclose(fp);
oldmanturtle 5:603c549bfefa 118 fp = fopen("/sd/mydir/sdtest.txt", "a");
oldmanturtle 4:327441ad8cf6 119 if(fp == NULL) {
oldmanturtle 8:25fc7a5cff17 120 ledError = true;
oldmanturtle 4:327441ad8cf6 121 error("Could not open file for write\n");
oldmanturtle 4:327441ad8cf6 122 }
oldmanturtle 5:603c549bfefa 123 pc.printf("\n\rSaved\n\r");
oldmanturtle 6:892ecb5fcfb9 124 sdMount = false;
oldmanturtle 3:9d9628bd3514 125 }