Thread: Stopwatch Function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    Stopwatch Function

    Hello, I am new to C/C++ programming, and was inquiring about creating a stopwatch function that starts at 0 sec and counts up to some time when a user tells it to stop. I am stuck and looking for somewhere to start.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This sort of application cannot be done in pure C, you will need to use OS specific functions. What OS are you using?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bithub View Post
    This sort of application cannot be done in pure C, you will need to use OS specific functions. What OS are you using?
    Down and dirty idea for this.

    About five times each second, your C program will get the current time from clock(), (this is just one way to do it, right off the top of my head, sans coffee), which you will subtract from an earlier time, to find and display the number of seconds that have elapsed, so far.

    This is just an idea, not a program ready to run:

    Code:
    //add any other include files you need
    #include <time.h>
    //#define ESC - trap the key press
    
    clock_t start, stop, loss;
    int keyPressed;
    start = clock();
    seconds1 = start / TICKS_PER_SEC;
    printf("\n\n Press ESC to Quit");
    do {
      
      /*pause in milliseconds (the OS will "schedule" the actual delay),
         giving it at least 200, but usually more)
      */
      delay(200);  
      stop = clock();
      start = stop + loss;  //See note
      seconds2 = stop  / TICKS_PER_SEC;
      if(seconds 2 > seconds1) {
        seconds1 = seconds2;
        total_sec++;
        
       // SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
        printf( Elapsed Time: %d seconds", total_sec);
      }
    }while(keyPressed != ESC);
    Your loop will lose a few milliseconds every loop, so you need to set loss to roughly adjust for it. How much you'll lose will depend on your OS and how busy your system is.

    Your OS may have high performance counters/timers, which can make your stopwatch more accurate.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Adak: Where is that delay() function coming from?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bithub View Post
    Adak: Where is that delay() function coming from?
    Oh Gawd! I slipped into Turbo C mode, didn't I?

    Salem was asking "Why would anyone want to use Turbo C?" For me, it's because TC lets me do these kind of little programs with one hand behind my back, blindfolded.


    Code:
    /* Adak, Sept 23, '09.
    stopwatch type program for Turbo C. Adjust loss or delay(), 
    for increased accuracy.
    
    Status: ok
    */
    
    #include <stdio.h>
    #include <time.h>      //for clock()
    #include <dos.h>       //for delay
    #include <conio.h>     //for getch()
    #define ESC 27  
    
    //27 = 0x1B
    //VK_ESCAPE (0x1B) ESC key- trap the key press maybe for Windows ?
    
    
    int main(void) {
      clock_t start, stop, loss;
      int i, seconds1, seconds2, total_sec, keyPress;
    
      loss = 20;
      total_sec = 0;
      start = clock();
      seconds1 = start / CLK_TCK;               //divide by TICKS_PER_SEC for Visual C;
      for(i = 0; i < 36; ++i)
         putchar('\n');
      gotoxy(10, 8);
      printf("Press ESC to Quit");
      do {
      
        /*pause in milliseconds (the OS will "schedule" the actual delay),
          giving it at least 200, but usually more)
        */
        delay(200);  
        stop = clock();
        start = stop + loss;  //See note
        seconds2 = stop  / CLK_TCK;    //TICKS_PER_SEC;
        if(seconds2 > seconds1) {
          seconds1 = seconds2;
          total_sec++;
        
         /* SetConsoleCursorPosition for Windows (Visual C), (or gotoxy() if you have 
            conio.h), is good for this:
         */
          gotoxy(10, 10);
          printf( "Elapsed Time: %d seconds", total_sec);
          if(kbhit())
            keyPress = getch();
        }
      } while(keyPress != ESC);
    
      printf("\n\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    It's apparently accurate for 60 seconds, in an off-hand test. It would need adjusting to be even close to accurate.

    This version gives a more "stopwatch" look to it:
    Code:
    /* Adak, Sept 23, '09.
    stopwatch type program for Turbo C. Adjust loss or delay(), 
    for increased accuracy.
    
    Status: ok
    */
    
    #include <stdio.h>
    #include <time.h>
    #include <dos.h>
    #include <conio.h>
    
    #define ESC 27  
    
    //27 = 0x1B
    //VK_ESCAPE (0x1B) ESC key- trap the key press
    
    
    int main(void) {
      clock_t start, stop, loss;
      int i, seconds1, seconds2, total_sec, keyPress;
    
      loss = 20;
      total_sec = 0;
      start = clock();
      seconds1 = start / CLK_TCK;               //TICKS_PER_SEC;
      for(i = 0; i < 36; ++i)
         putchar('\n');
      gotoxy(10, 8);
      printf("Press ESC to Quit");
      do {
      
        /*pause in milliseconds (the OS will "schedule" the actual delay),
          giving it at least what you request, but usually more)
        */
        delay(100);  
        stop = clock();
    //    start = stop + loss;  //See note
    //    seconds2 = stop  / CLK_TCK;    //TICKS_PER_SEC;
        gotoxy(10,10);
        printf("Elapsed Time: %.2f ", (stop-start)/CLK_TCK);
    /*
        if(seconds2 > seconds1) {
          seconds1 = seconds2;
          total_sec++;
        
         // SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
          gotoxy(10, 10);
          printf( "Elapsed Time: %d seconds", total_sec);
        }
    */
          if(kbhit())
            keyPress = getch();
    
      } while(keyPress != ESC);
    
      printf("\n\n\n\t\t\t     press enter when ready");
      seconds1 = getchar();
      return 0;
    }
    Last edited by Adak; 09-23-2009 at 02:37 PM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The error won't accumulate if you get the time from the system every time you print it.

    So say if time starts at 0s, and you want to print it out every 10s.
    (pseudo-code)
    Code:
    start_time = time();
    while (true) {
         elapsed = time() - start_time;
         print(elapsed);
         delay(10 - elapsed % 10); /* sleep the remaining time in the 10s (the previous sleep could have overslept) */
    }
    *edit* fixed a typo */edit*
    Last edited by cyberfish; 09-23-2009 at 10:06 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So it's self-adjusting, and has very little code. I *like* it.

    Very slick, Cyberfish!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM