02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Calculate.cpp

Committer:
aidanPJG
Date:
2015-07-08
Revision:
33:929f1755664b
Parent:
32:792c4239a738
Child:
38:da262dc4f90a

File content as of revision 33:929f1755664b:

#include "mbed.h"
#include <time.h>
#include <string>
#include <iostream>
//#include "Distance.h"

 //mbed pins
extern Serial pc;                                                                   //defines the communication between MBed and pc
int NoOfPins = 6;                                                                         //for how many sensors there are       
DigitalIn sensor[6] = {p5,p6,p7,p8,p9,p10} ;                                                 //array of sensors.  

 
//c++ variables
    time_t sensor_time ;                            //time at which sensor is broken
    int timeDiff;                          //time between 2 sensors - not used right now
    int times[6];                                //array the size of #pins
    float speed;
                                                  
int calculate(float distance)
{
    pc.printf("new program \n");                                            //alert user of initialisation   
      
    int i = 0; 
    while( i < 6)
    {
        while(!sensor[i]) 
        {
            if(sensor[i + 1]) {
                sensor_time = time(NULL); 
                i++; 
                }                                                         //error checking incase next one is done
        }
        sensor_time = time(NULL);                                                //gets current time
        pc.printf(" %dth sensor: %d \t", i,sensor_time);  
        pc.printf(" distance is: %f \t", distance); 
        times[i] = sensor_time;                                                 //adds sensor times to array for logging.
        if ( i > 0) {                                                       //to ensure it is not the first one
             timeDiff  = difftime(times[i], times[i-1]);                    //calculates the time difference
             pc.printf(" timediff: %d  \t", timeDiff); 
             speed = distance / timeDiff;
             pc.printf(" speed : %f  \t", speed); 
        }
        i++;
    } 
      pc.printf(" \n Calculate completed \n");                                      //alert to let user know it completed
      for (int i = 0; i < 5 ; i++)                                          //printing module for array
      {
        pc.printf(" times: %d  \t", times[i]); 
       }
      
}