Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

Committer:
oldmanturtle
Date:
Tue Apr 10 21:34:08 2018 +0000
Revision:
6:892ecb5fcfb9
Parent:
5:603c549bfefa
Child:
7:f7368fed0a2f
Version 1.0;

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 4:327441ad8cf6 11
oldmanturtle 5:603c549bfefa 12 //This is our timer
oldmanturtle 4:327441ad8cf6 13 Ticker countClock;
oldmanturtle 5:603c549bfefa 14
oldmanturtle 6:892ecb5fcfb9 15 ExtendedTimer timeClock;
oldmanturtle 6:892ecb5fcfb9 16
oldmanturtle 4:327441ad8cf6 17 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
oldmanturtle 2:c76b070c2a55 18
oldmanturtle 3:9d9628bd3514 19 Serial pc(USBTX,USBRX);
_laurentaylorrr 1:279fcab0c394 20
oldmanturtle 3:9d9628bd3514 21 float checkLightSensor(int n);
oldmanturtle 3:9d9628bd3514 22
oldmanturtle 5:603c549bfefa 23 //Switches the on states of the LEDs in the sphere
oldmanturtle 3:9d9628bd3514 24 void ledSwitch();
oldmanturtle 3:9d9628bd3514 25
oldmanturtle 5:603c549bfefa 26 //This should save the data to the sd card *This isn't working right now*
oldmanturtle 4:327441ad8cf6 27 void save();
oldmanturtle 4:327441ad8cf6 28
oldmanturtle 5:603c549bfefa 29 FILE *fp = NULL;
oldmanturtle 4:327441ad8cf6 30
oldmanturtle 6:892ecb5fcfb9 31 int main() {
oldmanturtle 6:892ecb5fcfb9 32 powerOn = 1;
oldmanturtle 6:892ecb5fcfb9 33 timeClock.start();
oldmanturtle 6:892ecb5fcfb9 34 sdMount = false;
oldmanturtle 6:892ecb5fcfb9 35 if (sd.mount() != 0) {
oldmanturtle 6:892ecb5fcfb9 36 pc.printf("Failed to mount the SD card.\r\n");
oldmanturtle 6:892ecb5fcfb9 37 sdMount = true;
oldmanturtle 6:892ecb5fcfb9 38 return -1; // ends program with error status
oldmanturtle 6:892ecb5fcfb9 39 }
oldmanturtle 6:892ecb5fcfb9 40
oldmanturtle 5:603c549bfefa 41 fp = fopen("/sd/mydir/sdtest.txt", "a");
oldmanturtle 6:892ecb5fcfb9 42
oldmanturtle 5:603c549bfefa 43 if(fp == NULL) {
oldmanturtle 6:892ecb5fcfb9 44 sdMount = true;
oldmanturtle 6:892ecb5fcfb9 45 ledError = true;
oldmanturtle 5:603c549bfefa 46 error("Could not open file for write\n");
oldmanturtle 6:892ecb5fcfb9 47 return -1;
oldmanturtle 5:603c549bfefa 48 }
oldmanturtle 6:892ecb5fcfb9 49
oldmanturtle 6:892ecb5fcfb9 50 int checkTimes = 60;
oldmanturtle 3:9d9628bd3514 51 ledRed = true;
oldmanturtle 3:9d9628bd3514 52 ledBlue = false;
oldmanturtle 4:327441ad8cf6 53 countClock.attach(&save, 10);
oldmanturtle 6:892ecb5fcfb9 54 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");
oldmanturtle 4:327441ad8cf6 55 while(true) {
oldmanturtle 5:603c549bfefa 56 //Blue Light
oldmanturtle 3:9d9628bd3514 57 ledSwitch();
oldmanturtle 4:327441ad8cf6 58 pc.printf("%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 6:892ecb5fcfb9 59 pc.printf("%.1f, ", timeClock.read());
oldmanturtle 4:327441ad8cf6 60 fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 6:892ecb5fcfb9 61 fprintf(fp,"%.1f, ", timeClock.read());
oldmanturtle 6:892ecb5fcfb9 62 wait(1);
oldmanturtle 5:603c549bfefa 63
oldmanturtle 5:603c549bfefa 64 //Red Light
oldmanturtle 3:9d9628bd3514 65 ledSwitch();
oldmanturtle 6:892ecb5fcfb9 66 pc.printf("%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 6:892ecb5fcfb9 67 pc.printf("%.1f\r\n ", timeClock.read());
oldmanturtle 6:892ecb5fcfb9 68 fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 6:892ecb5fcfb9 69 fprintf(fp,"%.1f\r\n", timeClock.read());
oldmanturtle 6:892ecb5fcfb9 70 wait(1);
oldmanturtle 4:327441ad8cf6 71 }
oldmanturtle 3:9d9628bd3514 72 }
oldmanturtle 3:9d9628bd3514 73
oldmanturtle 3:9d9628bd3514 74 // Average n readings of the light sensor
oldmanturtle 3:9d9628bd3514 75 float checkLightSensor(int n){
oldmanturtle 3:9d9628bd3514 76 float x;
oldmanturtle 3:9d9628bd3514 77 x = 0;
oldmanturtle 3:9d9628bd3514 78 for (int i = 0; i<n; i++)
oldmanturtle 3:9d9628bd3514 79 x = x + lightSensor;
oldmanturtle 3:9d9628bd3514 80 x = x/n;
oldmanturtle 3:9d9628bd3514 81 return x;
oldmanturtle 3:9d9628bd3514 82 }
oldmanturtle 3:9d9628bd3514 83
oldmanturtle 3:9d9628bd3514 84 void ledSwitch(){
oldmanturtle 3:9d9628bd3514 85 ledBlue = !ledBlue;
oldmanturtle 3:9d9628bd3514 86 ledRed = !ledRed;
oldmanturtle 4:327441ad8cf6 87 }
oldmanturtle 4:327441ad8cf6 88
oldmanturtle 4:327441ad8cf6 89 void save(){
oldmanturtle 4:327441ad8cf6 90 ledError = true;
oldmanturtle 6:892ecb5fcfb9 91 sdMount = true;
oldmanturtle 4:327441ad8cf6 92 fclose(fp);
oldmanturtle 5:603c549bfefa 93 fp = fopen("/sd/mydir/sdtest.txt", "a");
oldmanturtle 4:327441ad8cf6 94 if(fp == NULL) {
oldmanturtle 4:327441ad8cf6 95 error("Could not open file for write\n");
oldmanturtle 4:327441ad8cf6 96 ledError = true;
oldmanturtle 4:327441ad8cf6 97 }
oldmanturtle 5:603c549bfefa 98 pc.printf("\n\rSaved\n\r");
oldmanturtle 4:327441ad8cf6 99 ledError = false;
oldmanturtle 6:892ecb5fcfb9 100 sdMount = false;
oldmanturtle 3:9d9628bd3514 101 }