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.
main.cpp@10:07df00712f4e, 2018-04-26 (annotated)
- Committer:
- oldmanturtle
- Date:
- Thu Apr 26 17:25:13 2018 +0000
- Revision:
- 10:07df00712f4e
- Parent:
- 9:3cbb586b65b2
Final version;
Who changed what in which revision?
| User | Revision | Line number | New 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 | 10:07df00712f4e | 4 | //////////////////////////////////// |
| oldmanturtle | 10:07df00712f4e | 5 | //Declare all of the inputs and outputs |
| oldmanturtle | 10:07df00712f4e | 6 | //////////////////////////////////// |
| oldmanturtle | 4:327441ad8cf6 | 7 | |
| oldmanturtle | 10:07df00712f4e | 8 | //These are leds for humans to read |
| oldmanturtle | 10:07df00712f4e | 9 | DigitalOut ledError(LED3);//The internal sd error light |
| oldmanturtle | 10:07df00712f4e | 10 | DigitalOut powerOn(p16);//the external power light |
| oldmanturtle | 10:07df00712f4e | 11 | DigitalOut sdMount(p15);//the external sd error light |
| oldmanturtle | 10:07df00712f4e | 12 | |
| oldmanturtle | 10:07df00712f4e | 13 | //These are for the sphere |
| _laurentaylorrr | 0:46b2c955924b | 14 | AnalogIn lightSensor(p20); |
| _laurentaylorrr | 1:279fcab0c394 | 15 | DigitalOut ledRed(p25); |
| oldmanturtle | 2:c76b070c2a55 | 16 | DigitalOut ledBlue(p26); |
| oldmanturtle | 10:07df00712f4e | 17 | |
| oldmanturtle | 10:07df00712f4e | 18 | //These are for the tube |
| oldmanturtle | 9:3cbb586b65b2 | 19 | DigitalOut ledRedTube(p27); |
| oldmanturtle | 10:07df00712f4e | 20 | DigitalOut ledBlueTube(p28); |
| oldmanturtle | 9:3cbb586b65b2 | 21 | AnalogIn lightSensorTube(p19); |
| oldmanturtle | 4:327441ad8cf6 | 22 | |
| oldmanturtle | 5:603c549bfefa | 23 | //This is our timer |
| oldmanturtle | 4:327441ad8cf6 | 24 | Ticker countClock; |
| oldmanturtle | 5:603c549bfefa | 25 | |
| oldmanturtle | 10:07df00712f4e | 26 | //This is our clock |
| oldmanturtle | 6:892ecb5fcfb9 | 27 | ExtendedTimer timeClock; |
| oldmanturtle | 6:892ecb5fcfb9 | 28 | |
| oldmanturtle | 4:327441ad8cf6 | 29 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
| oldmanturtle | 2:c76b070c2a55 | 30 | |
| oldmanturtle | 10:07df00712f4e | 31 | Serial pc(USBTX,USBRX); //This communicates with the computer. We may want to eliminate this in our final code? |
| _laurentaylorrr | 1:279fcab0c394 | 32 | |
| oldmanturtle | 10:07df00712f4e | 33 | float checkLightSensor(int n);//Check the phototransistor in the sphere |
| oldmanturtle | 3:9d9628bd3514 | 34 | |
| oldmanturtle | 10:07df00712f4e | 35 | float checkTubeLightSensor(int n); //Check the phototransistor in the tube |
| oldmanturtle | 8:25fc7a5cff17 | 36 | |
| oldmanturtle | 5:603c549bfefa | 37 | //Switches the on states of the LEDs in the sphere |
| oldmanturtle | 3:9d9628bd3514 | 38 | void ledSwitch(); |
| oldmanturtle | 3:9d9628bd3514 | 39 | |
| oldmanturtle | 10:07df00712f4e | 40 | //This saves the data to the sd card |
| oldmanturtle | 4:327441ad8cf6 | 41 | void save(); |
| oldmanturtle | 4:327441ad8cf6 | 42 | |
| oldmanturtle | 10:07df00712f4e | 43 | FILE *fp = NULL; //This holds the location of the file. It is what we call when we write to the file. |
| oldmanturtle | 4:327441ad8cf6 | 44 | |
| oldmanturtle | 6:892ecb5fcfb9 | 45 | int main() { |
| oldmanturtle | 8:25fc7a5cff17 | 46 | powerOn = 1; //Turn on the power light on the module |
| oldmanturtle | 8:25fc7a5cff17 | 47 | timeClock.start(); //Start the clock to take the time |
| oldmanturtle | 8:25fc7a5cff17 | 48 | sdMount = false;//Set the sdMount light to default to off |
| oldmanturtle | 8:25fc7a5cff17 | 49 | |
| oldmanturtle | 8:25fc7a5cff17 | 50 | //This bit happens if the SD card doesn't mount |
| oldmanturtle | 6:892ecb5fcfb9 | 51 | if (sd.mount() != 0) { |
| oldmanturtle | 6:892ecb5fcfb9 | 52 | pc.printf("Failed to mount the SD card.\r\n"); |
| oldmanturtle | 6:892ecb5fcfb9 | 53 | sdMount = true; |
| oldmanturtle | 10:07df00712f4e | 54 | ledError = true; |
| oldmanturtle | 6:892ecb5fcfb9 | 55 | return -1; // ends program with error status |
| oldmanturtle | 6:892ecb5fcfb9 | 56 | } |
| oldmanturtle | 6:892ecb5fcfb9 | 57 | |
| oldmanturtle | 8:25fc7a5cff17 | 58 | //Open the file and append it |
| oldmanturtle | 5:603c549bfefa | 59 | fp = fopen("/sd/mydir/sdtest.txt", "a"); |
| oldmanturtle | 5:603c549bfefa | 60 | if(fp == NULL) { |
| oldmanturtle | 6:892ecb5fcfb9 | 61 | sdMount = true; |
| oldmanturtle | 6:892ecb5fcfb9 | 62 | ledError = true; |
| oldmanturtle | 5:603c549bfefa | 63 | error("Could not open file for write\n"); |
| oldmanturtle | 6:892ecb5fcfb9 | 64 | return -1; |
| oldmanturtle | 5:603c549bfefa | 65 | } |
| oldmanturtle | 8:25fc7a5cff17 | 66 | |
| oldmanturtle | 8:25fc7a5cff17 | 67 | int checkTimes = 60; //This determines how many times the led checks the phototransistor to get its averages |
| oldmanturtle | 10:07df00712f4e | 68 | |
| oldmanturtle | 10:07df00712f4e | 69 | //Initialize the lights |
| oldmanturtle | 3:9d9628bd3514 | 70 | ledRed = true; |
| oldmanturtle | 3:9d9628bd3514 | 71 | ledBlue = false; |
| oldmanturtle | 10:07df00712f4e | 72 | ledRedTube = true; |
| oldmanturtle | 10:07df00712f4e | 73 | ledBlueTube = false; |
| oldmanturtle | 10:07df00712f4e | 74 | |
| oldmanturtle | 10:07df00712f4e | 75 | countClock.attach(&save, 60); |
| oldmanturtle | 8:25fc7a5cff17 | 76 | fprintf(fp,"\r\n\r\n\r\n\r\nTimeBlueDataTaken,BlueLight,BlueLightTube,TimeRedDataTaken,RedLight,RedLightTube\n\r\n\r\n\r"); |
| oldmanturtle | 4:327441ad8cf6 | 77 | while(true) { |
| oldmanturtle | 10:07df00712f4e | 78 | //////////// |
| oldmanturtle | 8:25fc7a5cff17 | 79 | //Blue Lights |
| oldmanturtle | 10:07df00712f4e | 80 | //////////// |
| oldmanturtle | 10:07df00712f4e | 81 | ledSwitch(); //Switch the LEDs from off to on or vice versa |
| oldmanturtle | 10:07df00712f4e | 82 | |
| oldmanturtle | 10:07df00712f4e | 83 | //Print the data to the PC |
| oldmanturtle | 8:25fc7a5cff17 | 84 | pc.printf("%.1f,", timeClock.read()); |
| oldmanturtle | 8:25fc7a5cff17 | 85 | pc.printf("%.4f,", checkLightSensor(checkTimes)); |
| oldmanturtle | 8:25fc7a5cff17 | 86 | pc.printf("%.4f,", checkTubeLightSensor(checkTimes)); |
| oldmanturtle | 10:07df00712f4e | 87 | |
| oldmanturtle | 10:07df00712f4e | 88 | //Send the data to the sd card |
| oldmanturtle | 8:25fc7a5cff17 | 89 | fprintf(fp,"%.1f,", timeClock.read()); |
| oldmanturtle | 8:25fc7a5cff17 | 90 | fprintf(fp,"%.4f,", checkLightSensor(checkTimes)); |
| oldmanturtle | 8:25fc7a5cff17 | 91 | fprintf(fp,"%.4f,", checkTubeLightSensor(checkTimes)); |
| oldmanturtle | 7:f7368fed0a2f | 92 | |
| oldmanturtle | 10:07df00712f4e | 93 | wait(.5); //Wait one second |
| oldmanturtle | 9:3cbb586b65b2 | 94 | |
| oldmanturtle | 10:07df00712f4e | 95 | /////////// |
| oldmanturtle | 8:25fc7a5cff17 | 96 | //Red Lights |
| oldmanturtle | 10:07df00712f4e | 97 | /////////// |
| oldmanturtle | 10:07df00712f4e | 98 | ledSwitch(); //Switch all of the LEDs |
| oldmanturtle | 10:07df00712f4e | 99 | |
| oldmanturtle | 10:07df00712f4e | 100 | //Print the data to the computer |
| oldmanturtle | 9:3cbb586b65b2 | 101 | pc.printf("%.1f,", timeClock.read()); |
| oldmanturtle | 8:25fc7a5cff17 | 102 | pc.printf("%.4f,", checkLightSensor(checkTimes)); |
| oldmanturtle | 9:3cbb586b65b2 | 103 | pc.printf("%.4f\r\n", checkTubeLightSensor(checkTimes)); |
| oldmanturtle | 7:f7368fed0a2f | 104 | |
| oldmanturtle | 10:07df00712f4e | 105 | //Send the data to the sd card |
| oldmanturtle | 8:25fc7a5cff17 | 106 | fprintf(fp,"%.1f,", timeClock.read()); |
| oldmanturtle | 8:25fc7a5cff17 | 107 | fprintf(fp,"%.4f,", checkLightSensor(checkTimes)); |
| oldmanturtle | 10:07df00712f4e | 108 | fprintf(fp,"%.4f\r\n", checkTubeLightSensor(checkTimes)); |
| oldmanturtle | 8:25fc7a5cff17 | 109 | |
| oldmanturtle | 10:07df00712f4e | 110 | wait(9.5); |
| oldmanturtle | 4:327441ad8cf6 | 111 | } |
| oldmanturtle | 3:9d9628bd3514 | 112 | } |
| oldmanturtle | 3:9d9628bd3514 | 113 | |
| oldmanturtle | 3:9d9628bd3514 | 114 | // Average n readings of the light sensor |
| oldmanturtle | 3:9d9628bd3514 | 115 | float checkLightSensor(int n){ |
| oldmanturtle | 3:9d9628bd3514 | 116 | float x; |
| oldmanturtle | 3:9d9628bd3514 | 117 | x = 0; |
| oldmanturtle | 3:9d9628bd3514 | 118 | for (int i = 0; i<n; i++) |
| oldmanturtle | 3:9d9628bd3514 | 119 | x = x + lightSensor; |
| oldmanturtle | 3:9d9628bd3514 | 120 | x = x/n; |
| oldmanturtle | 3:9d9628bd3514 | 121 | return x; |
| oldmanturtle | 3:9d9628bd3514 | 122 | } |
| oldmanturtle | 3:9d9628bd3514 | 123 | |
| oldmanturtle | 8:25fc7a5cff17 | 124 | // Average n readings of the light sensor in the tube |
| oldmanturtle | 8:25fc7a5cff17 | 125 | float checkTubeLightSensor(int n){ |
| oldmanturtle | 8:25fc7a5cff17 | 126 | float x; |
| oldmanturtle | 8:25fc7a5cff17 | 127 | x = 0; |
| oldmanturtle | 8:25fc7a5cff17 | 128 | for (int i = 0; i<n; i++) |
| oldmanturtle | 8:25fc7a5cff17 | 129 | x = x + lightSensorTube; |
| oldmanturtle | 8:25fc7a5cff17 | 130 | x = x/n; |
| oldmanturtle | 8:25fc7a5cff17 | 131 | return x; |
| oldmanturtle | 8:25fc7a5cff17 | 132 | } |
| oldmanturtle | 8:25fc7a5cff17 | 133 | |
| oldmanturtle | 10:07df00712f4e | 134 | //Switch the values of all of the LEDs |
| oldmanturtle | 3:9d9628bd3514 | 135 | void ledSwitch(){ |
| oldmanturtle | 3:9d9628bd3514 | 136 | ledBlue = !ledBlue; |
| oldmanturtle | 3:9d9628bd3514 | 137 | ledRed = !ledRed; |
| oldmanturtle | 8:25fc7a5cff17 | 138 | ledBlueTube = !ledBlueTube; |
| oldmanturtle | 8:25fc7a5cff17 | 139 | ledRedTube = !ledRedTube; |
| oldmanturtle | 4:327441ad8cf6 | 140 | } |
| oldmanturtle | 4:327441ad8cf6 | 141 | |
| oldmanturtle | 10:07df00712f4e | 142 | //Save the data to the sd card |
| oldmanturtle | 4:327441ad8cf6 | 143 | void save(){ |
| oldmanturtle | 10:07df00712f4e | 144 | fclose(fp); //When the card is closed, the data actually saves |
| oldmanturtle | 10:07df00712f4e | 145 | fp = fopen("/sd/mydir/sdtest.txt", "a"); //This reopens the file, appending it |
| oldmanturtle | 10:07df00712f4e | 146 | |
| oldmanturtle | 10:07df00712f4e | 147 | //This triggers if the card cannot open |
| oldmanturtle | 4:327441ad8cf6 | 148 | if(fp == NULL) { |
| oldmanturtle | 10:07df00712f4e | 149 | sdMount = true;//This turns on the external mount error led |
| oldmanturtle | 10:07df00712f4e | 150 | ledError = true;//This turns on the internal mount error led |
| oldmanturtle | 4:327441ad8cf6 | 151 | error("Could not open file for write\n"); |
| oldmanturtle | 4:327441ad8cf6 | 152 | } |
| oldmanturtle | 5:603c549bfefa | 153 | pc.printf("\n\rSaved\n\r"); |
| oldmanturtle | 3:9d9628bd3514 | 154 | } |
