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().