![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Diff: Calculate.cpp
- Revision:
- 63:7d2bff227f65
- Parent:
- 62:9de96bcfdb4b
- Child:
- 64:809bd19f4e7c
diff -r 9de96bcfdb4b -r 7d2bff227f65 Calculate.cpp --- a/Calculate.cpp Tue Aug 04 14:37:54 2015 +0000 +++ b/Calculate.cpp Wed Aug 05 08:13:19 2015 +0000 @@ -18,6 +18,8 @@ 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 avgspeed = 0; double avgdistance = 0; @@ -28,40 +30,57 @@ 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]) //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++; + 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. + + if( i <1 ) //for the first one so it prints out 0 instead of getting inf from dividing by 0 and getting nan symbols. + { + printf("0\n"); + printf("0\n"); + printf("0\n"); + printf("0\n"); + printf("1\n"); + i++; + } + else + { - } - } - 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\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 - } - printArray(speeds,times,NoOfPins); + 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 + //*******average speed + 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 + + //******send to program. + 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 + //*********add to array + speeds[i] = speed; //log speeds. + avgSpeeds[i] = avgspeed; + totalTimes[i] = avgtime; + i++; //move onto next sensor + } + } + printArray(speeds,times,NoOfPins); } void printArray(double array[20], double timesArray[20], int NoOfPins) //for printing arrays to the terminal @@ -76,5 +95,4 @@ { data( i,times[i],speeds[i]); } - }