Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Calculate.cpp@44:3b45ec49bc44, 2015-07-08 (annotated)
- Committer:
- aidanPJG
- Date:
- Wed Jul 08 15:42:30 2015 +0000
- Revision:
- 44:3b45ec49bc44
- Parent:
- 39:3527578e78dd
- Child:
- 47:1dc3b5f7bf4a
tried to add speed as an array to print to xl but not quite there yet
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 | 38:da262dc4f90a | 7 | void printArray(int array[6]); |
| aidanPJG | 44:3b45ec49bc44 | 8 | void data(int sensor_number, int 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 | 26:4289549b2d21 | 12 | int NoOfPins = 6; //for how many sensors there are |
| aidanPJG | 31:bea1117f14fb | 13 | DigitalIn sensor[6] = {p5,p6,p7,p8,p9,p10} ; //array of sensors. |
| aidanPJG | 31:bea1117f14fb | 14 | |
| aidanPJG | 31:bea1117f14fb | 15 | |
| aidanPJG | 19:a3edf2c547e1 | 16 | //c++ variables |
| aidanPJG | 26:4289549b2d21 | 17 | time_t sensor_time ; //time at which sensor is broken |
| aidanPJG | 44:3b45ec49bc44 | 18 | int timeDiff; //time between 2 sensors - not used right now |
| aidanPJG | 44:3b45ec49bc44 | 19 | float speed; |
| aidanPJG | 31:bea1117f14fb | 20 | int times[6]; //array the size of #pins |
| aidanPJG | 44:3b45ec49bc44 | 21 | double speeds[6]; //array of speeds |
| aidanPJG | 33:929f1755664b | 22 | int calculate(float distance) |
| aidanPJG | 22:34bc2f2f5b09 | 23 | { |
| aidanPJG | 22:34bc2f2f5b09 | 24 | pc.printf("new program \n"); //alert user of initialisation |
| aidanPJG | 38:da262dc4f90a | 25 | |
| aidanPJG | 30:ab3a436e8968 | 26 | int i = 0; |
| aidanPJG | 30:ab3a436e8968 | 27 | while( i < 6) |
| aidanPJG | 25:bab86a12e9ad | 28 | { |
| aidanPJG | 30:ab3a436e8968 | 29 | while(!sensor[i]) |
| aidanPJG | 22:34bc2f2f5b09 | 30 | { |
| aidanPJG | 30:ab3a436e8968 | 31 | if(sensor[i + 1]) { |
| aidanPJG | 30:ab3a436e8968 | 32 | sensor_time = time(NULL); |
| aidanPJG | 30:ab3a436e8968 | 33 | i++; |
| aidanPJG | 30:ab3a436e8968 | 34 | } //error checking incase next one is done |
| aidanPJG | 22:34bc2f2f5b09 | 35 | } |
| aidanPJG | 33:929f1755664b | 36 | sensor_time = time(NULL); //gets current time |
| aidanPJG | 39:3527578e78dd | 37 | pc.printf("\n sensor %d : %d \t", i,sensor_time); |
| aidanPJG | 31:bea1117f14fb | 38 | times[i] = sensor_time; //adds sensor times to array for logging. |
| aidanPJG | 33:929f1755664b | 39 | if ( i > 0) { //to ensure it is not the first one |
| aidanPJG | 33:929f1755664b | 40 | timeDiff = difftime(times[i], times[i-1]); //calculates the time difference |
| aidanPJG | 39:3527578e78dd | 41 | pc.printf(" timediff: %d s \t", timeDiff); |
| aidanPJG | 33:929f1755664b | 42 | speed = distance / timeDiff; |
| aidanPJG | 39:3527578e78dd | 43 | pc.printf(" speed : %f m/s ", speed); |
| aidanPJG | 44:3b45ec49bc44 | 44 | speeds[i] = speed; |
| aidanPJG | 32:792c4239a738 | 45 | } |
| aidanPJG | 30:ab3a436e8968 | 46 | i++; |
| aidanPJG | 30:ab3a436e8968 | 47 | } |
| aidanPJG | 32:792c4239a738 | 48 | pc.printf(" \n Calculate completed \n"); //alert to let user know it completed |
| aidanPJG | 38:da262dc4f90a | 49 | printArray(times); |
| aidanPJG | 44:3b45ec49bc44 | 50 | //printArray(speeds); |
| aidanPJG | 38:da262dc4f90a | 51 | |
| aidanPJG | 22:34bc2f2f5b09 | 52 | } |
| aidanPJG | 30:ab3a436e8968 | 53 | |
| aidanPJG | 38:da262dc4f90a | 54 | void printArray(int array[6]) |
| aidanPJG | 38:da262dc4f90a | 55 | { |
| aidanPJG | 38:da262dc4f90a | 56 | for (int i = 0; i < 5 ; i++) //printing module for array |
| aidanPJG | 38:da262dc4f90a | 57 | { |
| aidanPJG | 39:3527578e78dd | 58 | pc.printf(" %d : %d \t", i,times[i]); |
| aidanPJG | 38:da262dc4f90a | 59 | } |
| aidanPJG | 44:3b45ec49bc44 | 60 | for (int i = 0; i < 5 ; i++) //printing to Datalog |
| aidanPJG | 44:3b45ec49bc44 | 61 | { |
| aidanPJG | 44:3b45ec49bc44 | 62 | data( i,times[i]); |
| aidanPJG | 44:3b45ec49bc44 | 63 | } |
| aidanPJG | 38:da262dc4f90a | 64 | |
| aidanPJG | 38:da262dc4f90a | 65 | } |