![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp@32:792c4239a738, 2015-07-08 (annotated)
- Committer:
- aidanPJG
- Date:
- Wed Jul 08 08:13:10 2015 +0000
- Revision:
- 32:792c4239a738
- Parent:
- 31:bea1117f14fb
- Child:
- 33:929f1755664b
time difference implemented;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aidanPJG | 22:34bc2f2f5b09 | 1 | #include "mbed.h" |
aidanPJG | 22:34bc2f2f5b09 | 2 | #include <time.h> |
aidanPJG | 22:34bc2f2f5b09 | 3 | #include <string> |
aidanPJG | 22:34bc2f2f5b09 | 4 | #include <iostream> |
aidanPJG | 31:bea1117f14fb | 5 | |
aidanPJG | 19:a3edf2c547e1 | 6 | //mbed pins |
aidanPJG | 26:4289549b2d21 | 7 | extern Serial pc; //defines the communication between MBed and pc |
aidanPJG | 26:4289549b2d21 | 8 | int NoOfPins = 6; //for how many sensors there are |
aidanPJG | 31:bea1117f14fb | 9 | DigitalIn sensor[6] = {p5,p6,p7,p8,p9,p10} ; //array of sensors. |
aidanPJG | 31:bea1117f14fb | 10 | |
aidanPJG | 31:bea1117f14fb | 11 | |
aidanPJG | 19:a3edf2c547e1 | 12 | //c++ variables |
aidanPJG | 26:4289549b2d21 | 13 | time_t sensor_time ; //time at which sensor is broken |
aidanPJG | 32:792c4239a738 | 14 | int timeDiff; //time between 2 sensors - not used right now |
aidanPJG | 31:bea1117f14fb | 15 | int times[6]; //array the size of #pins |
aidanPJG | 26:4289549b2d21 | 16 | |
aidanPJG | 30:ab3a436e8968 | 17 | int calculate() |
aidanPJG | 22:34bc2f2f5b09 | 18 | { |
aidanPJG | 22:34bc2f2f5b09 | 19 | pc.printf("new program \n"); //alert user of initialisation |
aidanPJG | 22:34bc2f2f5b09 | 20 | |
aidanPJG | 30:ab3a436e8968 | 21 | int i = 0; |
aidanPJG | 30:ab3a436e8968 | 22 | while( i < 6) |
aidanPJG | 25:bab86a12e9ad | 23 | { |
aidanPJG | 30:ab3a436e8968 | 24 | while(!sensor[i]) |
aidanPJG | 22:34bc2f2f5b09 | 25 | { |
aidanPJG | 30:ab3a436e8968 | 26 | if(sensor[i + 1]) { |
aidanPJG | 30:ab3a436e8968 | 27 | sensor_time = time(NULL); |
aidanPJG | 30:ab3a436e8968 | 28 | i++; |
aidanPJG | 30:ab3a436e8968 | 29 | } //error checking incase next one is done |
aidanPJG | 22:34bc2f2f5b09 | 30 | } |
aidanPJG | 30:ab3a436e8968 | 31 | sensor_time = time(NULL); |
aidanPJG | 32:792c4239a738 | 32 | pc.printf(" %dth sensor: %d \t", i,sensor_time); |
aidanPJG | 31:bea1117f14fb | 33 | times[i] = sensor_time; //adds sensor times to array for logging. |
aidanPJG | 32:792c4239a738 | 34 | if ( i > 0) { //to ensure it is not the first one |
aidanPJG | 32:792c4239a738 | 35 | // pc.printf(" time[i] : %d \t", times[i]); //testing |
aidanPJG | 32:792c4239a738 | 36 | // pc.printf(" time[i -1] : %d \t", times[i -1 ]); //testing |
aidanPJG | 32:792c4239a738 | 37 | timeDiff = difftime(times[i], times[i-1]); |
aidanPJG | 32:792c4239a738 | 38 | pc.printf(" timediff: %d \t", timeDiff); |
aidanPJG | 32:792c4239a738 | 39 | } |
aidanPJG | 30:ab3a436e8968 | 40 | i++; |
aidanPJG | 30:ab3a436e8968 | 41 | } |
aidanPJG | 32:792c4239a738 | 42 | pc.printf(" \n Calculate completed \n"); //alert to let user know it completed |
aidanPJG | 32:792c4239a738 | 43 | for (int i = 0; i < 5 ; i++) //printing module for array |
aidanPJG | 31:bea1117f14fb | 44 | { |
aidanPJG | 32:792c4239a738 | 45 | pc.printf(" times: %d \t", times[i]); |
aidanPJG | 31:bea1117f14fb | 46 | } |
aidanPJG | 25:bab86a12e9ad | 47 | |
aidanPJG | 22:34bc2f2f5b09 | 48 | } |
aidanPJG | 30:ab3a436e8968 | 49 | |
aidanPJG | 30:ab3a436e8968 | 50 |