Reputation: 13
I am trying to make a stopwatch in C to collect the time period of a pendulum. Although not shown here, the i
is a distance counter, the sensor measures the distance between it and the pendulum. So my objective was to start a timer when the distance between the pendulum and the sensor is less than or equal to 5. As at this distance the pendulum is directly above the sensor. And then to start the timer again when the pendulum comes back onto the sensor.
Doing this will give me the time period. In my attempt I decided to use the <time.h>
library and get the current time when the pendulum is on and off the sensor and then a subtraction to get the time period.
My code is below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int timeinfo;
int timeinfo1;
int timePeriod;
int main()
{
int i =0;
for (i=0;i<20;i++){
if (i<=5){
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) ); //Collecting the current time when i is less than 5
}
else{
time_t rawtime;
struct tm * timeinfo1;
time ( &rawtime );
timeinfo1 = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo1) );//Collecting the current time when i is greater than 5
}
}
timePeriod = timeinfo1-timeinfo;
printf("%s", timePeriod); // Calculating and printing the time period, the initial minus the final time.
}
The results of the code were not what I was expected and are shown as follows:
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
Current local time and date: Thu Jan 30 13:00:10 2020
(null)
Process returned 6 (0x6) execution time : 1.604 s
Press any key to continue.
As evident there is no time change in the values for each iteration and the final string value is returned as null. I think this is because subtracting strings is not possible, but I am not certain.
Upvotes: 1
Views: 241
Reputation: 157
First of all you define 'timeinfo' and 'timeinfo1' as global integer variables then re-define them as local variables in your code, why ?
Secondly the function you use (localtime) is giving you results with a granularity of second, since that for loop is executing very fast you -most likely- will never get a result bigger than 0 or 1. Therefore i suggest using clock_gettime function so that you can get a resolution up to nanoseconds.
Third problem is the way you print an integer using (%s)
I tried to fix your code as below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct timespec sub_tspec(struct timespec minuend, struct timespec subtrahend)
{
struct timespec result;
if (subtrahend.tv_nsec > minuend.tv_nsec) {
result.tv_sec = minuend.tv_sec - subtrahend.tv_sec - 1;
result.tv_nsec = (minuend.tv_nsec + 1e9) - subtrahend.tv_nsec;
} else {
result.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
result.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
}
return result;
}
int main()
{
int i;
struct timespec timePeriod;
struct timespec tspec1, tspec2;
time_t rawtime;
for (i = 0; i < 20; i++) {
if (i <= 5) {
clock_gettime(CLOCK_REALTIME, &tspec1);
printf("Current time: %ld.%ld\n", tspec1.tv_sec, tspec1.tv_nsec); //Collecting the current time when i is less than 5
} else {
clock_gettime(CLOCK_REALTIME, &tspec2);
printf("Current time: %ld.%ld\n", tspec2.tv_sec, tspec2.tv_nsec); //Collecting the current time when i is greater than 5
}
}
timePeriod = sub_tspec(tspec2, tspec1);
printf("%ld.%ld\n", timePeriod.tv_sec, timePeriod.tv_nsec); // Calculating and printing the time period, the initial minus the final time.
return 0;
}
You can find more detail about clock_gettime here: https://linux.die.net/man/3/clock_gettime
Upvotes: 2