Click here to Skip to main content
13,251,998 members (60,720 online)
Rate this:
Please Sign up or sign in to vote.
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 3-Oct-12 0:16am
Updated 3-Oct-12 0:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

Even I would stick with the sample code posted in the second answer link, if you want you can do something like:
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!
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


//input signal here
Joan Murt 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 Murt 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)

the function came out within 1 second.. @@
Joan Murt 3-Oct-12 6:54am
I don't understand what you are trying to do here...
If you want to simply wait x seconds without processing anything else in the middle then a Sleep call should be used.
Using a Timer is something that allow your pogram to continue running until it happens, moment in which the code inside the timer procedure will be called.

So: if you call settimer(1,1000,NULL); then you will call the timer procedure each second.
The other code won't wait, it will continue, if you plan to make the code to wait then you have to implement a loop or a sleep.

Probably I have not understood properly what you meant... :sigh:
lm1992 3-Oct-12 20:35pm
actually, i try to make a timer which set by an user, at the same the the machine got an input signal then it will kill the timer immediately..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100