Lauren Taylor / Mbed 2 deprecated phototransistor

Dependencies:   mbed

Committer:
oldmanturtle
Date:
Sat Mar 10 18:50:31 2018 +0000
Revision:
4:327441ad8cf6
Parent:
3:9d9628bd3514
Child:
5:603c549bfefa
Next Revision;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
_laurentaylorrr 0:46b2c955924b 1 #include "mbed.h"
oldmanturtle 4:327441ad8cf6 2 #include "SDFileSystem.h"
oldmanturtle 4:327441ad8cf6 3
_laurentaylorrr 0:46b2c955924b 4 AnalogIn lightSensor(p20);
_laurentaylorrr 1:279fcab0c394 5 DigitalOut ledRed(p25);
oldmanturtle 2:c76b070c2a55 6 DigitalOut ledBlue(p26);
oldmanturtle 4:327441ad8cf6 7 DigitalOut ledError(LED3);
oldmanturtle 4:327441ad8cf6 8
oldmanturtle 4:327441ad8cf6 9 Ticker countClock;
oldmanturtle 4:327441ad8cf6 10
oldmanturtle 4:327441ad8cf6 11 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
oldmanturtle 2:c76b070c2a55 12
oldmanturtle 3:9d9628bd3514 13 Serial pc(USBTX,USBRX);
_laurentaylorrr 1:279fcab0c394 14
oldmanturtle 3:9d9628bd3514 15 float checkLightSensor(int n);
oldmanturtle 3:9d9628bd3514 16
oldmanturtle 3:9d9628bd3514 17 void ledSwitch();
oldmanturtle 3:9d9628bd3514 18
oldmanturtle 4:327441ad8cf6 19 void save();
oldmanturtle 4:327441ad8cf6 20
oldmanturtle 4:327441ad8cf6 21 FILE *fp;
oldmanturtle 4:327441ad8cf6 22
_laurentaylorrr 0:46b2c955924b 23 int main() {
oldmanturtle 4:327441ad8cf6 24 mkdir("/sd/mydir", 0777);
oldmanturtle 4:327441ad8cf6 25 fp = fopen("/sd/mydir/sdtest.txt", "w");
oldmanturtle 3:9d9628bd3514 26 int checkTimes = 10;
oldmanturtle 3:9d9628bd3514 27 ledRed = true;
oldmanturtle 3:9d9628bd3514 28 ledBlue = false;
oldmanturtle 4:327441ad8cf6 29
oldmanturtle 4:327441ad8cf6 30
oldmanturtle 4:327441ad8cf6 31 if(fp == NULL) {
oldmanturtle 4:327441ad8cf6 32 error("Could not open file for write\n");
oldmanturtle 4:327441ad8cf6 33 ledError = true;
oldmanturtle 4:327441ad8cf6 34 }
oldmanturtle 4:327441ad8cf6 35 countClock.attach(&save, 10);
oldmanturtle 4:327441ad8cf6 36 while(true) {
oldmanturtle 3:9d9628bd3514 37 ledSwitch();
oldmanturtle 4:327441ad8cf6 38 pc.printf("%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 4:327441ad8cf6 39 fprintf(fp,"%.4f, ", checkLightSensor(checkTimes));
oldmanturtle 3:9d9628bd3514 40 wait(0.2);
oldmanturtle 3:9d9628bd3514 41 ledSwitch();
oldmanturtle 3:9d9628bd3514 42 pc.printf("%.4f\r\n", checkLightSensor(checkTimes));
oldmanturtle 4:327441ad8cf6 43 fprintf(fp,"%.4f\r\n", checkLightSensor(checkTimes));
_laurentaylorrr 0:46b2c955924b 44 wait(0.2);
oldmanturtle 4:327441ad8cf6 45 }
oldmanturtle 3:9d9628bd3514 46 }
oldmanturtle 3:9d9628bd3514 47
oldmanturtle 3:9d9628bd3514 48 // Average n readings of the light sensor
oldmanturtle 3:9d9628bd3514 49 float checkLightSensor(int n){
oldmanturtle 3:9d9628bd3514 50 float x;
oldmanturtle 3:9d9628bd3514 51 x = 0;
oldmanturtle 3:9d9628bd3514 52 for (int i = 0; i<n; i++)
oldmanturtle 3:9d9628bd3514 53 x = x + lightSensor;
oldmanturtle 3:9d9628bd3514 54 x = x/n;
oldmanturtle 3:9d9628bd3514 55 return x;
oldmanturtle 3:9d9628bd3514 56 }
oldmanturtle 3:9d9628bd3514 57
oldmanturtle 3:9d9628bd3514 58 void ledSwitch(){
oldmanturtle 3:9d9628bd3514 59 ledBlue = !ledBlue;
oldmanturtle 3:9d9628bd3514 60 ledRed = !ledRed;
oldmanturtle 4:327441ad8cf6 61 }
oldmanturtle 4:327441ad8cf6 62
oldmanturtle 4:327441ad8cf6 63 void save(){
oldmanturtle 4:327441ad8cf6 64 ledError = true;
oldmanturtle 4:327441ad8cf6 65 fclose(fp);
oldmanturtle 4:327441ad8cf6 66 fp = fopen("/sd/mydir/sdtest.txt", "w+");
oldmanturtle 4:327441ad8cf6 67 if(fp == NULL) {
oldmanturtle 4:327441ad8cf6 68 error("Could not open file for write\n");
oldmanturtle 4:327441ad8cf6 69 ledError = true;
oldmanturtle 4:327441ad8cf6 70 }
oldmanturtle 4:327441ad8cf6 71 fprintf(fp,"Saved\n\r");
oldmanturtle 4:327441ad8cf6 72 pc.printf("Saved\n\r");
oldmanturtle 4:327441ad8cf6 73 ledError = false;
oldmanturtle 3:9d9628bd3514 74 }