Code for Slot Sensor readout. Prints to text file on mbed.

Dependencies:   DebounceIn mbed

Committer:
kzampag
Date:
Wed Jul 10 19:23:13 2013 +0000
Revision:
6:360247c0c7df
Parent:
5:7e1dcb3904dd
Added comments to code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kzampag 0:6aa040f8bf2c 1 #include "mbed.h"
kzampag 6:360247c0c7df 2 /*
kzampag 6:360247c0c7df 3 Prints to terminal start and end time of each cycle. Writes times to txt file on mbed.
kzampag 6:360247c0c7df 4 Mbed acts as disconnected to computer when writing to local directory.
kzampag 6:360247c0c7df 5 Mbed will be return to device manager after test has completed.
kzampag 6:360247c0c7df 6 To regain access to mbed while test is running press "e" to terminate program.
kzampag 6:360247c0c7df 7
kzampag 6:360247c0c7df 8 Navigate to mbed after test has completed.
kzampag 6:360247c0c7df 9 Test results in file "test"
kzampag 6:360247c0c7df 10 File will be overwritten with each test. Rename file in code if want to keep multiple files
kzampag 6:360247c0c7df 11 */
kzampag 0:6aa040f8bf2c 12
kzampag 0:6aa040f8bf2c 13 Serial pc(USBTX, USBRX); // tx, rx
kzampag 0:6aa040f8bf2c 14
kzampag 0:6aa040f8bf2c 15
kzampag 4:9ea938dc72f8 16 DigitalIn sensor1(p5);
kzampag 4:9ea938dc72f8 17 DigitalIn sensor2(p6);
kzampag 3:a93f3494e6c1 18 LocalFileSystem local("local");
kzampag 0:6aa040f8bf2c 19
kzampag 0:6aa040f8bf2c 20 Timer t;
kzampag 2:c789547682d8 21 int change = 0;
kzampag 5:7e1dcb3904dd 22 int count = 1;
kzampag 3:a93f3494e6c1 23 const int endcount = 10;
kzampag 0:6aa040f8bf2c 24
kzampag 0:6aa040f8bf2c 25
kzampag 0:6aa040f8bf2c 26 int main()
kzampag 0:6aa040f8bf2c 27 {
kzampag 0:6aa040f8bf2c 28 pc.printf("Break sensor to start timer \n \r");
kzampag 3:a93f3494e6c1 29 FILE *fp = fopen("/local/test.txt", "w");
kzampag 2:c789547682d8 30
kzampag 2:c789547682d8 31
kzampag 5:7e1dcb3904dd 32 while(1) {
kzampag 2:c789547682d8 33 if (change == 0 && sensor1) {
kzampag 2:c789547682d8 34 change = 1;
kzampag 2:c789547682d8 35 t.reset();
kzampag 2:c789547682d8 36 t.start();
kzampag 5:7e1dcb3904dd 37 pc.printf("Start: Count= %d \n \r", count);
kzampag 2:c789547682d8 38 }
kzampag 0:6aa040f8bf2c 39
kzampag 0:6aa040f8bf2c 40
kzampag 2:c789547682d8 41 if (change && sensor2) {
kzampag 2:c789547682d8 42 t.stop();
kzampag 2:c789547682d8 43 pc.printf("End. Time taken was %f seconds \n \r", t.read());
kzampag 2:c789547682d8 44 change = 0;
kzampag 4:9ea938dc72f8 45 fprintf(fp, "%f \n", t.read());
kzampag 3:a93f3494e6c1 46 count++;
kzampag 3:a93f3494e6c1 47 }
kzampag 5:7e1dcb3904dd 48
kzampag 5:7e1dcb3904dd 49 if (count>endcount) {
kzampag 3:a93f3494e6c1 50 pc.printf("Done");
kzampag 3:a93f3494e6c1 51 fclose(fp);
kzampag 3:a93f3494e6c1 52 return 0;
kzampag 5:7e1dcb3904dd 53 }
kzampag 3:a93f3494e6c1 54
kzampag 5:7e1dcb3904dd 55 if (pc.readable()) {
kzampag 5:7e1dcb3904dd 56 char c = pc.getc();
kzampag 5:7e1dcb3904dd 57 if(c == 'e') {
kzampag 5:7e1dcb3904dd 58 pc.printf("Program terminated");
kzampag 5:7e1dcb3904dd 59 return 0;
kzampag 5:7e1dcb3904dd 60 }
kzampag 2:c789547682d8 61 }
kzampag 2:c789547682d8 62
kzampag 0:6aa040f8bf2c 63
kzampag 5:7e1dcb3904dd 64
kzampag 0:6aa040f8bf2c 65 }
kzampag 0:6aa040f8bf2c 66 }