Thread: Time Delay

  1. #1
    Boaz
    Guest

    Talking Time Delay

    Hi, everyone. Right is may turn out to be really simple thing to do, but i am not sure how to approach it.

    I have to develop a traffic light simulator, for a junction (consisting of one major busy road and one minor), now the problem is have to doing the traffic lights.

    I want to know is it possible to execute code within C repeatly over and over for a certain amount of time (i.e how long the each traffic lights will operate for), before moving on to some other code?

    PS. any ideas on how to approach the above project (traffic light simulator), would be help full.

    regards
    Boaz

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I want to know is it possible to execute code within C repeatly over and over for a certain amount of time (i.e how long the each traffic lights will operate for), before moving on to some other code?
    Sure, just set the on/off flag for the light and then call a sleep function like sleep int unistd.h, Sleep in windows.h or delay from dos.h depending on your system for however long you need. If you ARE the system you have should have control of the embedded timing procedures. As a last resort you can build your own sleep function inside C, but it's pretty antisocial since it blocks everything on a mulitasking system. As such, I don't recommend using things like the following unless you have control and don't mind assuming that you're the most important program running.
    Code:
    void SLEEP(long length)
    {
        clock_t done = clock() + (clock_t)(length * CLOCKS_PER_SEC);
    
        while (clock() < done)
            ;
    }

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    If he is in DOS, multitasking won't matter too much...

    Delay is simple enough... I implemented in some code I stuck someplace and forgot where... Somebody needs to add a code button in the reply.

    Code:
    delay(number_of_milliseconds);

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. Time delay function
    By sundeeptuteja in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2003, 05:17 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM