Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 10:07df00712f4e
- Parent:
- 9:3cbb586b65b2
--- a/main.cpp Thu Apr 12 15:46:31 2018 +0000 +++ b/main.cpp Thu Apr 26 17:25:13 2018 +0000 @@ -1,37 +1,46 @@ #include "mbed.h" #include "ExtendedTimer.h" #include "SDFileSystem.h" +//////////////////////////////////// +//Declare all of the inputs and outputs +//////////////////////////////////// +//These are leds for humans to read +DigitalOut ledError(LED3);//The internal sd error light +DigitalOut powerOn(p16);//the external power light +DigitalOut sdMount(p15);//the external sd error light + +//These are for the sphere AnalogIn lightSensor(p20); DigitalOut ledRed(p25); DigitalOut ledBlue(p26); -DigitalOut ledError(LED3); -DigitalOut powerOn(p16); -DigitalOut sdMount(p15); + +//These are for the tube DigitalOut ledRedTube(p27); -DigitalOut ledBlueTube(p26); +DigitalOut ledBlueTube(p28); AnalogIn lightSensorTube(p19); //This is our timer Ticker countClock; +//This is our clock ExtendedTimer timeClock; SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board -Serial pc(USBTX,USBRX); +Serial pc(USBTX,USBRX); //This communicates with the computer. We may want to eliminate this in our final code? -float checkLightSensor(int n); +float checkLightSensor(int n);//Check the phototransistor in the sphere -float checkTubeLightSensor(int n); +float checkTubeLightSensor(int n); //Check the phototransistor in the tube //Switches the on states of the LEDs in the sphere void ledSwitch(); -//This should save the data to the sd card *This isn't working right now* +//This saves the data to the sd card void save(); -FILE *fp = NULL; +FILE *fp = NULL; //This holds the location of the file. It is what we call when we write to the file. int main() { powerOn = 1; //Turn on the power light on the module @@ -42,6 +51,7 @@ if (sd.mount() != 0) { pc.printf("Failed to mount the SD card.\r\n"); sdMount = true; + ledError = true; return -1; // ends program with error status } @@ -55,33 +65,49 @@ } int checkTimes = 60; //This determines how many times the led checks the phototransistor to get its averages + + //Initialize the lights ledRed = true; ledBlue = false; - countClock.attach(&save, 120); + ledRedTube = true; + ledBlueTube = false; + + countClock.attach(&save, 60); fprintf(fp,"\r\n\r\n\r\n\r\nTimeBlueDataTaken,BlueLight,BlueLightTube,TimeRedDataTaken,RedLight,RedLightTube\n\r\n\r\n\r"); while(true) { + //////////// //Blue Lights - ledSwitch(); + //////////// + ledSwitch(); //Switch the LEDs from off to on or vice versa + + //Print the data to the PC pc.printf("%.1f,", timeClock.read()); pc.printf("%.4f,", checkLightSensor(checkTimes)); pc.printf("%.4f,", checkTubeLightSensor(checkTimes)); + + //Send the data to the sd card fprintf(fp,"%.1f,", timeClock.read()); fprintf(fp,"%.4f,", checkLightSensor(checkTimes)); fprintf(fp,"%.4f,", checkTubeLightSensor(checkTimes)); - wait(1); + wait(.5); //Wait one second + /////////// //Red Lights - ledSwitch(); + /////////// + ledSwitch(); //Switch all of the LEDs + + //Print the data to the computer pc.printf("%.1f,", timeClock.read()); pc.printf("%.4f,", checkLightSensor(checkTimes)); pc.printf("%.4f\r\n", checkTubeLightSensor(checkTimes)); + //Send the data to the sd card fprintf(fp,"%.1f,", timeClock.read()); fprintf(fp,"%.4f,", checkLightSensor(checkTimes)); - fprintf(fp,"%.4f\n\r", checkTubeLightSensor(checkTimes)); + fprintf(fp,"%.4f\r\n", checkTubeLightSensor(checkTimes)); - wait(1); + wait(9.5); } } @@ -105,6 +131,7 @@ return x; } +//Switch the values of all of the LEDs void ledSwitch(){ ledBlue = !ledBlue; ledRed = !ledRed; @@ -112,14 +139,16 @@ ledRedTube = !ledRedTube; } +//Save the data to the sd card void save(){ - sdMount = true; - fclose(fp); - fp = fopen("/sd/mydir/sdtest.txt", "a"); + fclose(fp); //When the card is closed, the data actually saves + fp = fopen("/sd/mydir/sdtest.txt", "a"); //This reopens the file, appending it + + //This triggers if the card cannot open if(fp == NULL) { - ledError = true; + sdMount = true;//This turns on the external mount error led + ledError = true;//This turns on the internal mount error led error("Could not open file for write\n"); } pc.printf("\n\rSaved\n\r"); - sdMount = false; } \ No newline at end of file