Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

Committer:
oldmanturtle
Date:
Tue Apr 10 21:35:08 2018 +0000
Revision:
7:f7368fed0a2f
Parent:
6:892ecb5fcfb9
Child:
8:25fc7a5cff17
v1.1 Changelog: Edit data values output (switched time to the left and data to the right;

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 7:f7368fed0a2f 58 pc.printf("%.1f, ", timeClock.read());
oldmanturtle 4:327441ad8cf6 59 pc.printf("%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 60 fprintf(fp,"%.1f, ", timeClock.read());
oldmanturtle 4:327441ad8cf6 61 fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 62
oldmanturtle 6:892ecb5fcfb9 63 wait(1);
oldmanturtle 5:603c549bfefa 64
oldmanturtle 5:603c549bfefa 65 //Red Light
oldmanturtle 3:9d9628bd3514 66 ledSwitch();
oldmanturtle 7:f7368fed0a2f 67 pc.printf("%.1f\r\n ", timeClock.read());
oldmanturtle 6:892ecb5fcfb9 68 pc.printf("%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 69 fprintf(fp,"%.1f\r\n", timeClock.read());
oldmanturtle 6:892ecb5fcfb9 70 fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 7:f7368fed0a2f 71
oldmanturtle 6:892ecb5fcfb9 72 wait(1);
oldmanturtle 4:327441ad8cf6 73 }
oldmanturtle 3:9d9628bd3514 74 }
oldmanturtle 3:9d9628bd3514 75
oldmanturtle 3:9d9628bd3514 76 // Average n readings of the light sensor
oldmanturtle 3:9d9628bd3514 77 float checkLightSensor(int n){
oldmanturtle 3:9d9628bd3514 78 float x;
oldmanturtle 3:9d9628bd3514 79 x = 0;
oldmanturtle 3:9d9628bd3514 80 for (int i = 0; i<n; i++)
oldmanturtle 3:9d9628bd3514 81 x = x + lightSensor;
oldmanturtle 3:9d9628bd3514 82 x = x/n;
oldmanturtle 3:9d9628bd3514 83 return x;
oldmanturtle 3:9d9628bd3514 84 }
oldmanturtle 3:9d9628bd3514 85
oldmanturtle 3:9d9628bd3514 86 void ledSwitch(){
oldmanturtle 3:9d9628bd3514 87 ledBlue = !ledBlue;
oldmanturtle 3:9d9628bd3514 88 ledRed = !ledRed;
oldmanturtle 4:327441ad8cf6 89 }
oldmanturtle 4:327441ad8cf6 90
oldmanturtle 4:327441ad8cf6 91 void save(){
oldmanturtle 4:327441ad8cf6 92 ledError = true;
oldmanturtle 6:892ecb5fcfb9 93 sdMount = true;
oldmanturtle 4:327441ad8cf6 94 fclose(fp);
oldmanturtle 5:603c549bfefa 95 fp = fopen("/sd/mydir/sdtest.txt", "a");
oldmanturtle 4:327441ad8cf6 96 if(fp == NULL) {
oldmanturtle 4:327441ad8cf6 97 error("Could not open file for write\n");
oldmanturtle 4:327441ad8cf6 98 ledError = true;
oldmanturtle 4:327441ad8cf6 99 }
oldmanturtle 5:603c549bfefa 100 pc.printf("\n\rSaved\n\r");
oldmanturtle 4:327441ad8cf6 101 ledError = false;
oldmanturtle 6:892ecb5fcfb9 102 sdMount = false;
oldmanturtle 3:9d9628bd3514 103 }