Thread: How to execute Time Loop in C without clock() and timer ?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    5

    How to execute Time Loop in C without clock() and timer ?

    I want to execute 2 different output at a certain time in seconds. And change the swap time using keyEvent. Here's the idea.

    How to execute Time Loop in C without clock() and timer ?-gambar2-jpg

    I've been looking for some advice online and I only find those with timer and clock(). I don't need to use timer and clock() since I already use a utilTimer library to count milliseconds as the program is executed.
    Here's the my keyEvent code :

    Code:
        if (key == '1' ) {
            mod=10;
        }
    
    
        if (key == '2' ) {
            mod=5;
        }
        
        if (key == '3' ) {
            mod=3;
        }
        
        if (key == '4' ) {
            mod=1;
        }
    How do I implement the time loop ? As of now for the looping I'm using mod, but still a little bit confused and haven't achieved my goal.

    Code:
    for (int count = 0; true; count++) {
        if (count % 10 == 0) {
            //every 10 seconds
        }
        if (count % 5 == 0) {
            // Every 5 seconds
        }
    }
    Any advice ?
    Last edited by pixie_laluna; 4 Days Ago at 01:10 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,629
    What is this 'utilTimer' you're talking about?

    Can it be used to call a function when a timer expires?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,192
    Without knowing what utilTimer is, we're reduced to guessing; but it seems clear that you need to actually use utilTimer, instead of just counting by ones via count++ (since the probability that your loop takes exactly one millisecond is ... slim). How you use utilTimer depends on what it is; if it gives you an absolute time, you'll be subtracting a starting value to find time elapsed; if it gives you time since the previous call you'll be adding to a total; if it does something else, well, then you'll do something else.

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Without knowing what utilTimer is, we're reduced to guessing; but it seems clear that you need to actually use utilTimer, instead of just counting by ones via count++ (since the probability that your loop takes exactly one millisecond is ... slim). How you use utilTimer depends on what it is; if it gives you an absolute time, you'll be subtracting a starting value to find time elapsed; if it gives you time since the previous call you'll be adding to a total; if it does something else, well, then you'll do something else.
    Thanks ! Right now I'm doing exactly that. Subtracting elapsed time with the time when I hit the key and compute if it bigger or less than my frequency that I defined in my keyEvent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clock ticks vs time.h
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 01-26-2010, 11:46 AM
  2. High Speed IRQ Timer/Clock in C
    By andrewwan1980 in forum C Programming
    Replies: 7
    Last Post: 01-16-2008, 05:21 AM
  3. Real Time Clock
    By peckitt99 in forum C++ Programming
    Replies: 15
    Last Post: 11-06-2007, 08:54 AM
  4. Real time clock
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 12:22 PM
  5. time measurement using clock() and time()
    By kuhnmi in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2004, 06:32 AM

Tags for this Thread