![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp@58:c729d48cbeef, 2015-07-29 (annotated)
- Committer:
- aidanPJG
- Date:
- Wed Jul 29 14:10:31 2015 +0000
- Revision:
- 58:c729d48cbeef
- Parent:
- 57:f94834892570
- Child:
- 59:2cbbd6fea55b
not much
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 | 39:3527578e78dd | 5 | #include "Distance.h" |
aidanPJG | 31:bea1117f14fb | 6 | |
aidanPJG | 57:f94834892570 | 7 | void printArray(double array[20], double timesArray[20], int NoOfPins); |
aidanPJG | 57:f94834892570 | 8 | void data(int sensor_number, double time, double speed); |
aidanPJG | 38:da262dc4f90a | 9 | |
aidanPJG | 19:a3edf2c547e1 | 10 | //mbed pins |
aidanPJG | 26:4289549b2d21 | 11 | extern Serial pc; //defines the communication between MBed and pc |
aidanPJG | 53:10f4f519ba69 | 12 | //int NoOfPins = 3; //for how many sensors there are |
aidanPJG | 52:d21fc7266b66 | 13 | DigitalIn sensor[20] = {p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24} ; //array of sensors. |
aidanPJG | 31:bea1117f14fb | 14 | |
aidanPJG | 31:bea1117f14fb | 15 | |
aidanPJG | 19:a3edf2c547e1 | 16 | //c++ variables |
aidanPJG | 58:c729d48cbeef | 17 | |
aidanPJG | 56:bf08d9e50ccc | 18 | Timer t; |
aidanPJG | 56:bf08d9e50ccc | 19 | double timeDiff; //time between 2 sensors - not used right now |
aidanPJG | 57:f94834892570 | 20 | double speed; |
aidanPJG | 57:f94834892570 | 21 | double times[20]; //array the size of #pins |
aidanPJG | 52:d21fc7266b66 | 22 | double speeds[20]; //array of speeds |
aidanPJG | 52:d21fc7266b66 | 23 | |
aidanPJG | 52:d21fc7266b66 | 24 | |
aidanPJG | 52:d21fc7266b66 | 25 | |
aidanPJG | 53:10f4f519ba69 | 26 | int calculate(double distance, int NoOfPins) |
aidanPJG | 22:34bc2f2f5b09 | 27 | { |
aidanPJG | 53:10f4f519ba69 | 28 | |
aidanPJG | 22:34bc2f2f5b09 | 29 | pc.printf("new program \n"); //alert user of initialisation |
aidanPJG | 38:da262dc4f90a | 30 | |
aidanPJG | 30:ab3a436e8968 | 31 | int i = 0; |
aidanPJG | 52:d21fc7266b66 | 32 | while( i < NoOfPins) |
aidanPJG | 25:bab86a12e9ad | 33 | { |
aidanPJG | 30:ab3a436e8968 | 34 | while(!sensor[i]) |
aidanPJG | 22:34bc2f2f5b09 | 35 | { |
aidanPJG | 56:bf08d9e50ccc | 36 | if(sensor[i + 1]) |
aidanPJG | 56:bf08d9e50ccc | 37 | { |
aidanPJG | 57:f94834892570 | 38 | printf("it skipped sensor % d", i); |
aidanPJG | 30:ab3a436e8968 | 39 | i++; |
aidanPJG | 56:bf08d9e50ccc | 40 | |
aidanPJG | 30:ab3a436e8968 | 41 | } //error checking incase next one is done |
aidanPJG | 22:34bc2f2f5b09 | 42 | } |
aidanPJG | 57:f94834892570 | 43 | t.start(); //starts the timer |
aidanPJG | 57:f94834892570 | 44 | timeDiff = t.read(); //reads what the timer is currently at |
aidanPJG | 57:f94834892570 | 45 | t.reset(); //starts the timer again from 0. |
aidanPJG | 57:f94834892570 | 46 | times[i] = timeDiff; //adds sensor times to array for logging. |
aidanPJG | 57:f94834892570 | 47 | pc.printf(" timediff: %lf s \t", timeDiff); |
aidanPJG | 56:bf08d9e50ccc | 48 | if ( i > 0) |
aidanPJG | 56:bf08d9e50ccc | 49 | { //to ensure it is not the first one |
aidanPJG | 56:bf08d9e50ccc | 50 | speed = distance / timeDiff; |
aidanPJG | 56:bf08d9e50ccc | 51 | pc.printf(" speed : %f m/s ", speed); |
aidanPJG | 56:bf08d9e50ccc | 52 | speeds[i] = speed; |
aidanPJG | 32:792c4239a738 | 53 | } |
aidanPJG | 30:ab3a436e8968 | 54 | i++; |
aidanPJG | 30:ab3a436e8968 | 55 | } |
aidanPJG | 32:792c4239a738 | 56 | pc.printf(" \n Calculate completed \n"); //alert to let user know it completed |
aidanPJG | 57:f94834892570 | 57 | printArray(speeds, times, NoOfPins); |
aidanPJG | 38:da262dc4f90a | 58 | |
aidanPJG | 22:34bc2f2f5b09 | 59 | } |
aidanPJG | 30:ab3a436e8968 | 60 | |
aidanPJG | 57:f94834892570 | 61 | void printArray(double array[20], double timesArray[20], int NoOfPins) |
aidanPJG | 38:da262dc4f90a | 62 | { |
aidanPJG | 52:d21fc7266b66 | 63 | for (int i = 0; i < NoOfPins ; i++) //printing module for array |
aidanPJG | 38:da262dc4f90a | 64 | { |
aidanPJG | 57:f94834892570 | 65 | pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]); |
aidanPJG | 50:d794595c6868 | 66 | // pc.printf(" %d : %d \t", i,times[i]); |
aidanPJG | 38:da262dc4f90a | 67 | } |
aidanPJG | 52:d21fc7266b66 | 68 | for (int i = 0; i < NoOfPins ; i++) //printing to Datalog |
aidanPJG | 44:3b45ec49bc44 | 69 | { |
aidanPJG | 51:210353276e06 | 70 | data( i,times[i],speeds[i]); |
aidanPJG | 44:3b45ec49bc44 | 71 | } |
aidanPJG | 38:da262dc4f90a | 72 | |
aidanPJG | 38:da262dc4f90a | 73 | } |