Cosmic Ray Sheilding
Dependencies: mbed SDFileSystem ExtendedTimer
main.cpp@4:66eb0ab160b5, 2019-04-04 (annotated)
- Committer:
- ccalderon22
- Date:
- Thu Apr 04 18:06:06 2019 +0000
- Revision:
- 4:66eb0ab160b5
- Parent:
- 3:466fdc08e0cd
Finished Code!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ccalderon22 | 4:66eb0ab160b5 | 1 | /* Cosmic Ray Sheilding |
ccalderon22 | 4:66eb0ab160b5 | 2 | Date Created: Spring Semester 2019 |
ccalderon22 | 4:66eb0ab160b5 | 3 | |
ccalderon22 | 4:66eb0ab160b5 | 4 | Program to detect number of Geiger Counter hits and read data to SDcard |
ccalderon22 | 4:66eb0ab160b5 | 5 | The time since program start and the voltage |
ccalderon22 | 4:66eb0ab160b5 | 6 | are written to a tab-delimited data file. |
ccalderon22 | 4:66eb0ab160b5 | 7 | SD pins: DI = p5, DO = p6, SCK = p7, CS = p8 |
ccalderon22 | 4:66eb0ab160b5 | 8 | */ |
ccalderon22 | 0:600099ef57c8 | 9 | |
ccalderon22 | 4:66eb0ab160b5 | 10 | #include "mbed.h" |
ccalderon22 | 4:66eb0ab160b5 | 11 | #include "SDFileSystem.h" |
ccalderon22 | 4:66eb0ab160b5 | 12 | #include "ExtendedTimer.h" |
ccalderon22 | 4:66eb0ab160b5 | 13 | |
ccalderon22 | 4:66eb0ab160b5 | 14 | InterruptIn geigerDelrin(p14); //Counter Serial Number Ending in:101 |
ccalderon22 | 4:66eb0ab160b5 | 15 | InterruptIn geigerJello(p13); //Counter Serial Number Ending in:142 |
ccalderon22 | 0:600099ef57c8 | 16 | Serial pc (USBTX, USBRX); |
ccalderon22 | 4:66eb0ab160b5 | 17 | SDFileSystem fs(p5, p6, p7, p8, "fs"); |
ccalderon22 | 4:66eb0ab160b5 | 18 | DigitalOut doneLED(LED1); |
ccalderon22 | 4:66eb0ab160b5 | 19 | |
ccalderon22 | 4:66eb0ab160b5 | 20 | ExtendedTimer t; |
ccalderon22 | 4:66eb0ab160b5 | 21 | Ticker sampleTime; |
ccalderon22 | 4:66eb0ab160b5 | 22 | Ticker saveTime; |
ccalderon22 | 0:600099ef57c8 | 23 | |
ccalderon22 | 0:600099ef57c8 | 24 | void countDelrinPulse(void); //Function called when Delrin Geiger reads pulse |
ccalderon22 | 0:600099ef57c8 | 25 | void countJelloPulse(void); //Function called when Jello Geiger reads pulse |
ccalderon22 | 0:600099ef57c8 | 26 | |
ccalderon22 | 0:600099ef57c8 | 27 | int jelloCount = 0; |
ccalderon22 | 0:600099ef57c8 | 28 | int delrinCount = 0; |
ccalderon22 | 4:66eb0ab160b5 | 29 | bool timeToSample; |
ccalderon22 | 4:66eb0ab160b5 | 30 | bool timeToSave; |
ccalderon22 | 0:600099ef57c8 | 31 | |
ccalderon22 | 4:66eb0ab160b5 | 32 | void triggerSample() { |
ccalderon22 | 4:66eb0ab160b5 | 33 | timeToSave = true; // flag to save data |
ccalderon22 | 4:66eb0ab160b5 | 34 | } |
ccalderon22 | 4:66eb0ab160b5 | 35 | |
ccalderon22 | 4:66eb0ab160b5 | 36 | void triggerCollection() { |
ccalderon22 | 4:66eb0ab160b5 | 37 | timeToSample = true; // flag to collect data |
ccalderon22 | 4:66eb0ab160b5 | 38 | } |
ccalderon22 | 0:600099ef57c8 | 39 | |
ccalderon22 | 4:66eb0ab160b5 | 40 | int main(){ |
ccalderon22 | 4:66eb0ab160b5 | 41 | //Geiger Counter Interrupts |
ccalderon22 | 4:66eb0ab160b5 | 42 | geigerDelrin.fall(&countDelrinPulse); |
ccalderon22 | 4:66eb0ab160b5 | 43 | geigerJello.fall(&countJelloPulse); |
ccalderon22 | 4:66eb0ab160b5 | 44 | |
ccalderon22 | 4:66eb0ab160b5 | 45 | // Mount the filesystem |
ccalderon22 | 4:66eb0ab160b5 | 46 | bool mountFailure = fs.mount(); |
ccalderon22 | 4:66eb0ab160b5 | 47 | |
ccalderon22 | 4:66eb0ab160b5 | 48 | if (mountFailure != 0) { |
ccalderon22 | 4:66eb0ab160b5 | 49 | pc.printf("Failed to mount the SD card.\r\n"); |
ccalderon22 | 4:66eb0ab160b5 | 50 | return -1; // ends program with error status |
ccalderon22 | 4:66eb0ab160b5 | 51 | } |
ccalderon22 | 4:66eb0ab160b5 | 52 | FILE* fp = fopen("/fs/log.txt","w"); |
ccalderon22 | 4:66eb0ab160b5 | 53 | if (fp == NULL) { |
ccalderon22 | 4:66eb0ab160b5 | 54 | pc.printf("Failed to open the file.\r\n"); |
ccalderon22 | 4:66eb0ab160b5 | 55 | fs.unmount(); |
ccalderon22 | 4:66eb0ab160b5 | 56 | return -1; |
ccalderon22 | 4:66eb0ab160b5 | 57 | } |
ccalderon22 | 0:600099ef57c8 | 58 | |
ccalderon22 | 4:66eb0ab160b5 | 59 | // Data header |
ccalderon22 | 4:66eb0ab160b5 | 60 | pc.printf("Time (s) \t Jello Count \t Delrin Count \r\n"); |
ccalderon22 | 4:66eb0ab160b5 | 61 | fprintf(fp, "Time (s) \t Jello Count \t Delrin Count \r\n"); |
ccalderon22 | 4:66eb0ab160b5 | 62 | |
ccalderon22 | 4:66eb0ab160b5 | 63 | // Attach ISR(s) to tickers |
ccalderon22 | 4:66eb0ab160b5 | 64 | sampleTime.attach(&triggerSample, 10); //Sample every 60 seconds |
ccalderon22 | 4:66eb0ab160b5 | 65 | saveTime.attach(&triggerCollection, 30); //Save data every 5 minutes. |
ccalderon22 | 4:66eb0ab160b5 | 66 | |
ccalderon22 | 4:66eb0ab160b5 | 67 | timeToSample = true; |
ccalderon22 | 4:66eb0ab160b5 | 68 | timeToSave = false; |
ccalderon22 | 4:66eb0ab160b5 | 69 | |
ccalderon22 | 4:66eb0ab160b5 | 70 | // Start the timer and ticker |
ccalderon22 | 4:66eb0ab160b5 | 71 | t.start(); |
ccalderon22 | 4:66eb0ab160b5 | 72 | |
ccalderon22 | 4:66eb0ab160b5 | 73 | while (t.read() < 18000) { |
ccalderon22 | 4:66eb0ab160b5 | 74 | if (timeToSample){ //Displays number of counts per 60 seconds |
ccalderon22 | 4:66eb0ab160b5 | 75 | timeToSample = false; |
ccalderon22 | 4:66eb0ab160b5 | 76 | pc.printf("%.2f \t\t %d \t\t %d \r\n",t.read(), jelloCount, delrinCount); |
ccalderon22 | 4:66eb0ab160b5 | 77 | fprintf(fp, "%.2f \t\t %d \t\t %d \r\n",t.read(), jelloCount, delrinCount); |
ccalderon22 | 4:66eb0ab160b5 | 78 | jelloCount = 0; |
ccalderon22 | 4:66eb0ab160b5 | 79 | delrinCount = 0; |
ccalderon22 | 4:66eb0ab160b5 | 80 | } |
ccalderon22 | 4:66eb0ab160b5 | 81 | |
ccalderon22 | 4:66eb0ab160b5 | 82 | if (timeToSave){ //Data will save every five minutes |
ccalderon22 | 4:66eb0ab160b5 | 83 | timeToSave = false; |
ccalderon22 | 4:66eb0ab160b5 | 84 | fclose(fp); |
ccalderon22 | 4:66eb0ab160b5 | 85 | fp = fopen("/fs/log.txt","a"); |
ccalderon22 | 4:66eb0ab160b5 | 86 | } |
ccalderon22 | 0:600099ef57c8 | 87 | } |
ccalderon22 | 4:66eb0ab160b5 | 88 | |
ccalderon22 | 4:66eb0ab160b5 | 89 | // Close file and unmount the file system |
ccalderon22 | 4:66eb0ab160b5 | 90 | fclose(fp); |
ccalderon22 | 4:66eb0ab160b5 | 91 | |
ccalderon22 | 4:66eb0ab160b5 | 92 | fs.unmount(); |
ccalderon22 | 4:66eb0ab160b5 | 93 | // LED on indicates that it is safe to remove the SD card |
ccalderon22 | 4:66eb0ab160b5 | 94 | doneLED = 1; |
ccalderon22 | 4:66eb0ab160b5 | 95 | } |
lwells | 2:2c78fc509a01 | 96 | |
ccalderon22 | 4:66eb0ab160b5 | 97 | //Incrementation of counts |
lwells | 3:466fdc08e0cd | 98 | void countDelrinPulse(void){ |
lwells | 2:2c78fc509a01 | 99 | delrinCount++; |
lwells | 2:2c78fc509a01 | 100 | } |
lwells | 2:2c78fc509a01 | 101 | |
lwells | 3:466fdc08e0cd | 102 | void countJelloPulse(void){ |
lwells | 3:466fdc08e0cd | 103 | jelloCount++; |
ccalderon22 | 0:600099ef57c8 | 104 | } |