02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Calculate.cpp

Committer:
aidanPJG
Date:
2015-07-13
Revision:
47:1dc3b5f7bf4a
Parent:
44:3b45ec49bc44
Child:
50:d794595c6868

File content as of revision 47:1dc3b5f7bf4a:

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

void printArray(int array[3]);
void data(int sensor_number, int speed);

 //mbed pins
extern Serial pc;                                                                   //defines the communication between MBed and pc
int NoOfPins = 3;                                                                         //for how many sensors there are       
DigitalIn sensor[3] = {p5,p6,p7} ;                                                 //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   
    float speed;
    int times[3];                                //array the size of #pins
    double speeds[6];                                //array of speeds            
int calculate(float distance)
{
    pc.printf("new program \n");                                            //alert user of initialisation   
     
    int i = 0; 
    while( i < 3)
    {
        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("\n sensor %d : %d \t", i,sensor_time);  
        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 s \t", timeDiff); 
             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(times);
      //printArray(speeds);
  
}

void printArray(int array[3])
      {
        for (int i = 0; i < 3 ; i++)                                     //printing module for array                              
        {
             pc.printf(" %d : %d  \t", i,times[i]); 
         }
         for (int i = 0; i < 3 ; i++)                                     //printing to Datalog                            
        {
             data( i,times[i]); 
         }
         
      }