![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Diff: Calculate.cpp
- Revision:
- 64:809bd19f4e7c
- Parent:
- 63:7d2bff227f65
- Child:
- 68:c6399471ea49
diff -r 7d2bff227f65 -r 809bd19f4e7c Calculate.cpp --- a/Calculate.cpp Wed Aug 05 08:13:19 2015 +0000 +++ b/Calculate.cpp Wed Aug 05 08:44:12 2015 +0000 @@ -4,25 +4,29 @@ #include <iostream> #include "Distance.h" -void printArray(double array[20], double timesArray[20], int NoOfPins); //defining the methods -void data(int sensor_number, double time, double speed); -DigitalOut led4(LED4); - +//*************defining methods used in this method +void printArray(double array[20], double timesArray[20], int NoOfPins,double speedAvg[20], double timesTotal[20]); +void data(int sensor_number, double time, double speed, double timesTotal, double speedAvg); + //*********mbed pins********************************* extern Serial pc; //defines the communication between MBed and pc 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. +DigitalOut led4(LED4); -//********c++ variables******************************* +//********c++ variables******************************* + Timer t; - double timeDiff= 0; //time between 2 sensors - not used right now - double speed = 0; - double times[20]; //array the size of #pins - double speeds[20]; //array of speeds - double avgSpeeds[20]; - double totalTimes[20]; - double avgtime= 0;; + double timeDiff= 0; //time between 2 sensors + double speed = 0; //instspeed + double avgtime= 0;; double avgspeed = 0; double avgdistance = 0; + //arrays + double times[20]; //array of the time the sensor is reached .(the size of #pins) + double speeds[20]; //array of inst speeds + double avgSpeeds[20]; //avg speeds + double totalTimes[20]; //total runnning times + int calculate(double distance, int NoOfPins) @@ -43,7 +47,7 @@ t.start(); //starts the timer once, doesn't do anything next iteration 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. + times[i] = timeDiff; //adds sensor times to array for logging. this must remain above the calculations for avgdistance to work. if( i <1 ) //for the first one so it prints out 0 instead of getting inf from dividing by 0 and getting nan symbols. { @@ -80,10 +84,10 @@ i++; //move onto next sensor } } - printArray(speeds,times,NoOfPins); + printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes); } -void printArray(double array[20], double timesArray[20], int NoOfPins) //for printing arrays to the terminal +void printArray(double array[20], double timesArray[20], int NoOfPins, double speedAvg[20], double timesTotal[20]) //for printing arrays to the terminal { /*prints to the terminal for (int i = 0; i < NoOfPins ; i++) //printing module for array @@ -93,6 +97,6 @@ } */ for (int i = 0; i < NoOfPins ; i++) //printing to Datalog { - data( i,times[i],speeds[i]); + data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]); } }