Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Confuse how to make set a timer for control the progress of a machine.
For an Example, if I would like to make my progress of machine like this, I insert something into the machine, withing x second, if the machine did not get any input signal, the buzzer/ alarm will activate automatically, the x second i mean is define by the user..and another question is.. how to set the timer for the progress, for an example, I want to let my machine to wash Object in 2 Minutes time, what should I do with it?
Posted
Updated 2-Oct-12 23:23pm
v3

1 solution

FIRST ANSWER
It all depends on what you have to do between times:

If you don't have to react to events meanwhile time is passing you can use Sleep().
If you need to react to something meanwhile time is passing... then it will be a little bit more difficult.

You can set timers[^] (see also[^]).

Keep in mind that the timer itself won't do all the job, you will have to continue handling the events you expect to happen, the timer will make it easier to know when the time has passed.

Probably putting the code to handle events inside a loop will be enough for you.

SECOND ANSWER
Your question/comment look like this one: automotive machine timer[^]. See the answer there.

Apart of that, if you want to be able to react to any event meanwhile you are waiting that desired amount of time, you can't do that... See my other answer (link posted before) where I explain a little bit how to react to those behaviors.

You should do it by putting a small time base (t10ms) and each time the time base has happened then check the conditions (and add up each cycle the amount of time in order to be able to check for the maximum allowed time) or put a timer (SetTimer) and react to a variable while waiting for the events (you could kill the timer each time the desired event fires).
...

THIRD ANSWER
Even I would stick with the sample code posted in the second answer link, if you want you can do something like:
C++
m_nTimer = SetTimer(1, x*1000, NULL);  // to know which timer is the one you've just set and being able to kill it later.
while ((not bLimit) and (not bInputsignal))  // wait for the desired signal and for the input in one loop.
{
 sleep(10); // to avoid killing your CPU.
}
KillTimer(m_nTimer); // once you've left the loop (conditions fulfilled) then you can kill the timer as it acts as a timeout only.

In your timer routine you should modify the bLimit value to TRUE.

Good luck!
 
Share this answer
 
v5
Comments
lm1992 3-Oct-12 5:42am    
Hi Juan,
I got a question, OK for and example, from 'Station A' to 'Station B', i will set a time limit as 5 second, within 5 second, if my machine failed to get any input signal, it will pop out an error message
So can I make it like this?

// some function here

Sleep(x*1000);

if(x>x*1000)
{
MessageBox(NULL,NULL,NULL);
}
else
{
//input signal here
}
Joan M 3-Oct-12 6:01am    
See my updated answer. ;)
lm1992 3-Oct-12 6:12am    
Thanks , er do u mind i ask u another question bout the SetTimer function?
Joan M 3-Oct-12 6:29am    
Of course not, ask as many questions you want, I'll answer if I know what to say... :)
lm1992 3-Oct-12 6:33am    
thx.. :)
actually just now i have make an experiment of SetTimer.
but I did not get the result as what I want,

this is the sample code,
// some code here
SetTimer( 1, x*1000, NULL );

if(x *= 1000)
{
//function
}

the function came out within 1 second.. @@

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900