Flashing!

This is a discussion on Flashing! within the C Programming forums, part of the General Programming Boards category; Hello, im trying to use a delay of some form in a C program but i cannot find out how ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    4

    Question Flashing!

    Hello, im trying to use a delay of some form in a C program but i cannot find out how to do one!

    I am trying to make a light flash on a piece of hardware, i have worked out i need to do this by using a loop, with some for of delay so it makes the light go on for a bit wait.. then off, wait again and loop this cycle for so long until the rest of my program comes in!

    I gather there must be a single command to do this but i cant find one!

    Can someone please help me!

    thanks!

    Mike


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The only portable way to do this is grossly antisocial, you should look in the libraries available to you for a sleep, delay, pause, or wait function that does what you want. However, if you really want the portable method, here is one way to do it:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void mypause ( int sec )
    {
      clock_t end = clock() + ( sec * CLOCKS_PER_SEC );
    
      while ( clock() < end )
        ;
    }
    
    void flash ( int n )
    {
      while ( --n >= 0 ) {
        printf ( "*\r" );
        mypause ( 1 );
        printf ( " \r" );
        mypause ( 1 );
      }
    }
    
    int main ( void )
    {
      flash ( 5 );
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    the clock() function is very slow and should be avoided at all costs.

    Especilly if your looking to use the code in an embedded enviroment.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    4


    Who needs social? ive been in uni working solid for the past 3 days! not good!

    thanks!

    Mike

  5. #5
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Make a loop which wastes time, although the speed of the CPU will make times differ.
    Code:
    #define DELAY (50000)
    
    
    int junk;
    
    for (junk=0; junk<DELAY;junk++){ }

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    4
    Ill just try them all!

    i think a time dely suits my purposes better!

    Mike


  7. #7
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28
    It depends on which OS you program for, if it's a POSIX compilant one, you can use sleep, or usleep in *BSD or Linux. If you program for windows, i think there's a Sleep() function (?), but i'm not sure. It depends on which programming environment you're using too.
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    4
    Its windows! and we are using some dofdy enviroment that my lecturer said to use because its free, dont like it personally but got moaned at for using another last time.....

    Im trying them all seeing whiich is the best! thanks all!

    Mike


  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    There is another function GetClockTickCount() or something like that which will be more accurate. You can then divide the ticks by 1,000 or the resolution of your timer on your machine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flashing text
    By Queatrix in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 10:32 PM
  2. Flashing when minimized
    By b00l34n in forum Windows Programming
    Replies: 6
    Last Post: 10-10-2004, 11:54 AM
  3. Flashing screen?????
    By Joe100 in forum Game Programming
    Replies: 2
    Last Post: 10-29-2003, 03:03 AM
  4. Is it possible to create flashing text?
    By wolzi in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2002, 09:53 PM
  5. Print without flashing cursor
    By johnnyd in forum C Programming
    Replies: 4
    Last Post: 04-02-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21