Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC
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 2-Oct-12 23:16pm
lm1992295
Edited 2-Oct-12 23:23pm
v3

1 solution

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

Solution 1

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:
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!
  Permalink  
v5
Comments
lm1992 at 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 Murt at 3-Oct-12 6:01am
   
See my updated answer. ;)
lm1992 at 3-Oct-12 6:12am
   
Thanks , er do u mind i ask u another question bout the SetTimer function?
Joan Murt at 3-Oct-12 6:29am
   
Of course not, ask as many questions you want, I'll answer if I know what to say... :)
lm1992 at 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.. @@
Joan Murt at 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 at 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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 576
1 OriginalGriff 183
2 Tadit Dash 160
3 Sergey Alexandrovich Kryukov 136
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,280
1 OriginalGriff 5,473
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid