timer problem

This is a discussion on timer problem within the C Programming forums, part of the General Programming Boards category; I have a hardware board with a motion detector. I can read the detector with the function int pirGet(). It ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    timer problem

    I have a hardware board with a motion detector. I can read the detector with the function int pirGet(). It returns 0 or 1

    I want to make a function that returns the status of the last 10 seconds.

    If motion is detected the output of the function must remain 1 for 10 seconds.

    I want to read the function at any time.. anyone got an idea how to do this ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    This would depend on your OS / Compiler / program type....
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by robin171
    I have a hardware board with a motion detector. I can read the detector with the function int pirGet(). It returns 0 or 1

    I want to make a function that returns the status of the last 10 seconds.

    If motion is detected the output of the function must remain 1 for 10 seconds.

    I want to read the function at any time.. anyone got an idea how to do this ?
    timeGetTime() would be your best choice for this. What it does is returns the time in milliseconds from when the application started to the current time. The conversion from milliseconds to seconds would be t = t * 1/1000 where t = time. You can now think of delta time or 'dt' as the time passed or the 'difference' from the last call to timeGetTime() to the current call. So, instincts tell you dt would equal the difference of the 2 calls, so: dt = currTime - lastTime;. For example, its 1:00pm when you called timeGetTime() last, and its 1:01pm now, 1:01-1:00 would be 1 minute - easy right? To put it into code:

    Code:
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    
    float LastTime = (float)timeGetTime();
    float TimeElapsed = 0.0f;
    
    while( msg.message != WM_QUIT )
    {
        if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else // Loop through your code
        {
            float CurrTime = (float)timeGetTime();
            float DeltaTime = max( (CurrTime-LastTime)*.001f, 0.0f );
            TimeElapsed += DeltaTime;
    
            // Do your code
    
            if( TimeElapsed >= 10 ) // Ten seconds passed :)
            {
                   // Do your code
                  TimeElapsed = 0.0f;
            }
             
            LastTime = CurrTime;
         }
    }
    That should give you a general idea of timeGetTime().
    Last edited by durban; 10-17-2005 at 09:08 AM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. design problem
    By hannibar in forum C Programming
    Replies: 6
    Last Post: 11-17-2005, 05:22 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. Help needed with external call, WinExec() and timer.
    By algorithm in forum Windows Programming
    Replies: 9
    Last Post: 11-07-2001, 08:35 AM

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