02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Revision:
61:e7f2a0e38360
Parent:
60:27404fc0dc71
Child:
62:9de96bcfdb4b
diff -r 27404fc0dc71 -r e7f2a0e38360 Calculate.cpp
--- a/Calculate.cpp	Thu Jul 30 14:08:27 2015 +0000
+++ b/Calculate.cpp	Tue Aug 04 10:24:39 2015 +0000
@@ -4,67 +4,75 @@
 #include <iostream>
 #include "Distance.h"
 
-void printArray(double array[20], double timesArray[20], int NoOfPins);
-void data(int sensor_number, double time, double speed);
+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);                                                 
 
- //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.  
+ //*********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.  
 
- 
-//c++ variables
-   
+//********c++ variables*******************************   
     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     
-    
+    double speeds[20];                                //array of speeds 
+    double avgtime;    
+    double avgspeed;
+    double avgdistance;
 
       
 int calculate(double distance, int NoOfPins)
-{
-    
-    //pc.printf("new program \n");                                            //alert user of initialisation   
-     
-    int i = 0; 
-    while( i < NoOfPins)
+{     
+    int i = 0;                                          //set the pin to 0, starting from the 0th ( first) pin
+    while( i < NoOfPins)                                //while we have not reached the last pin
     {
-        while(!sensor[i]) 
-        {
-            if(sensor[i + 1])
-             {
-                //printf("it skipped sensor % d", i);
-                i++; 
-                
-                }                                                         //error checking incase next one is done
-        }
+            while(!sensor[i])                                       //while the sensor is not broken hold it. 
+            {
+                if(sensor[i + 1])                                       //bit of error bypassing
+                 {
+                    //printf("it skipped sensor % d", i);
+                    led4 = 1;                                           //alerts user that a sensor has been skipped. 
+                    i++; 
+                    
+                }                                                        
+            }
         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.
-      //  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 \n", speed); 
-                 speeds[i] = speed;
-        }
-        i++;
+        
+       pc.printf(" timediff: %lf\n", timeDiff); //testing
+
+               
+                                                              //to ensure it is not the first one
+                 speed = 60* (distance / timeDiff);                //multiplied by 60 to get meters per minute
+                 avgdistance = distance * i;                        //total distance = distance between each sensor multiplied by number of sensors
+                 // pc.printf("%f dist/s \n", avgdistance);   //testing  
+               
+                    avgtime = times[1] + times[2] + times[3] + times[4] + times[5] + times[6] + times[7] + times[8] + times[9] +times[10] + times[11] + times[12]+times[13] + times[14] + times[15]+times[16] + times[17] + times[18]+ times[19] ;                      //total time sum of all times
+                    //gets the total time it has taken. Could start another timer and only read it.
+                 pc.printf("avgtime: %f \n", avgtime);  //testing
+                avgspeed = 60*(avgdistance / avgtime);                  //multiplied by 60 to get meters per minute
+              
+                 pc.printf("%f\n", speed);            //inst speed printed  
+                 pc.printf("%f\n", avgspeed);        //avg speed printed
+                 pc.printf("%d \n",i+1);                //which sensor it is on printed
+                 speeds[i] = speed;                     //log speeds.
+             i++;                           //move onto next sensor
     } 
-      pc.printf(" \n Calculate completed \n");                                      //alert to let user know it completed
-     // printArray(speeds, times, NoOfPins);
-  
+    printArray(speeds,times,NoOfPins);
 }
 
-void printArray(double array[20], double timesArray[20], int NoOfPins)
+void printArray(double array[20], double timesArray[20], int NoOfPins)              //for printing arrays to the terminal
       {
+        /*prints to the terminal
         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]);