![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp
- Committer:
- aidanPJG
- Date:
- 2015-07-28
- Revision:
- 57:f94834892570
- Parent:
- 56:bf08d9e50ccc
- Child:
- 58:c729d48cbeef
File content as of revision 57:f94834892570:
#include "mbed.h" #include <time.h> #include <string> #include <iostream> #include "Distance.h" void printArray(double array[20], double timesArray[20], int NoOfPins); void data(int sensor_number, double time, double speed); //mbed pins extern Serial pc; //defines the communication between MBed and pc //int NoOfPins = 3; //for how many sensors there are 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. //c++ variables time_t sensor_time ; //time at which sensor is broken Timer t; double timeDiff; //time between 2 sensors - not used right now double speed; double times[20]; //array the size of #pins double speeds[20]; //array of speeds int calculate(double distance, int NoOfPins) { pc.printf("new program \n"); //alert user of initialisation int i = 0; while( i < NoOfPins) { while(!sensor[i]) { if(sensor[i + 1]) { printf("it skipped sensor % d", i); i++; } //error checking incase next one is done } t.start(); //starts the timer timeDiff = t.read(); //reads what the timer is currently at t.reset(); //starts the timer again from 0. times[i] = timeDiff; //adds sensor times to array for logging. pc.printf(" timediff: %lf s \t", timeDiff); if ( i > 0) { //to ensure it is not the first one speed = distance / timeDiff; pc.printf(" speed : %f m/s ", speed); speeds[i] = speed; } i++; } pc.printf(" \n Calculate completed \n"); //alert to let user know it completed printArray(speeds, times, NoOfPins); } void printArray(double array[20], double timesArray[20], int NoOfPins) { for (int i = 0; i < NoOfPins ; i++) //printing module for array { pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]); // pc.printf(" %d : %d \t", i,times[i]); } for (int i = 0; i < NoOfPins ; i++) //printing to Datalog { data( i,times[i],speeds[i]); } }