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

Dependencies:   DebounceIn mbed

Committer:
kzampag
Date:
Mon Jul 08 16:43:29 2013 +0000
Revision:
5:7e1dcb3904dd
Parent:
4:9ea938dc72f8
Child:
6:360247c0c7df
Manual terminate added. Counter added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kzampag 0:6aa040f8bf2c 1 #include "mbed.h"
kzampag 0:6aa040f8bf2c 2
kzampag 0:6aa040f8bf2c 3 Serial pc(USBTX, USBRX); // tx, rx
kzampag 0:6aa040f8bf2c 4
kzampag 0:6aa040f8bf2c 5
kzampag 4:9ea938dc72f8 6 DigitalIn sensor1(p5);
kzampag 4:9ea938dc72f8 7 DigitalIn sensor2(p6);
kzampag 3:a93f3494e6c1 8 LocalFileSystem local("local");
kzampag 0:6aa040f8bf2c 9
kzampag 0:6aa040f8bf2c 10 Timer t;
kzampag 2:c789547682d8 11 int change = 0;
kzampag 5:7e1dcb3904dd 12 int count = 1;
kzampag 3:a93f3494e6c1 13 const int endcount = 10;
kzampag 0:6aa040f8bf2c 14
kzampag 0:6aa040f8bf2c 15
kzampag 0:6aa040f8bf2c 16 int main()
kzampag 0:6aa040f8bf2c 17 {
kzampag 0:6aa040f8bf2c 18 pc.printf("Break sensor to start timer \n \r");
kzampag 3:a93f3494e6c1 19 FILE *fp = fopen("/local/test.txt", "w");
kzampag 2:c789547682d8 20
kzampag 2:c789547682d8 21
kzampag 5:7e1dcb3904dd 22 while(1) {
kzampag 2:c789547682d8 23 if (change == 0 && sensor1) {
kzampag 2:c789547682d8 24 change = 1;
kzampag 2:c789547682d8 25 t.reset();
kzampag 2:c789547682d8 26 t.start();
kzampag 5:7e1dcb3904dd 27 pc.printf("Start: Count= %d \n \r", count);
kzampag 2:c789547682d8 28 }
kzampag 0:6aa040f8bf2c 29
kzampag 0:6aa040f8bf2c 30
kzampag 2:c789547682d8 31 if (change && sensor2) {
kzampag 2:c789547682d8 32 t.stop();
kzampag 2:c789547682d8 33 pc.printf("End. Time taken was %f seconds \n \r", t.read());
kzampag 2:c789547682d8 34 change = 0;
kzampag 4:9ea938dc72f8 35 fprintf(fp, "%f \n", t.read());
kzampag 3:a93f3494e6c1 36 count++;
kzampag 3:a93f3494e6c1 37 }
kzampag 5:7e1dcb3904dd 38
kzampag 5:7e1dcb3904dd 39 if (count>endcount) {
kzampag 3:a93f3494e6c1 40 pc.printf("Done");
kzampag 3:a93f3494e6c1 41 fclose(fp);
kzampag 3:a93f3494e6c1 42 return 0;
kzampag 5:7e1dcb3904dd 43 }
kzampag 3:a93f3494e6c1 44
kzampag 5:7e1dcb3904dd 45 if (pc.readable()) {
kzampag 5:7e1dcb3904dd 46 char c = pc.getc();
kzampag 5:7e1dcb3904dd 47 if(c == 'e') {
kzampag 5:7e1dcb3904dd 48 pc.printf("Program terminated");
kzampag 5:7e1dcb3904dd 49 return 0;
kzampag 5:7e1dcb3904dd 50 }
kzampag 2:c789547682d8 51 }
kzampag 2:c789547682d8 52
kzampag 0:6aa040f8bf2c 53
kzampag 5:7e1dcb3904dd 54
kzampag 0:6aa040f8bf2c 55 }
kzampag 0:6aa040f8bf2c 56 }