updating output value with timer and resetting if the timer exceeds a certain time or it has a new input value
-
Good morning everyone. I am a newbie in c++ coding and recently encountered a problem related to timer. Note: i am with MFC, so I can use the timer from MFC. but not sure how to about it. what I have to do is looks something like this; if (input = 1) { start timer for 300ms ; output = 1 ; } if ( new input within 300ms = 1) { restart the timer for 300ms; output = 1; } if (input != 1 after 300ms || time elasped > 300ms ) { output = 0; } I have a a stream of input 0 and 1 coming from an array. requirement is that after the program received an input of 1 from the array, it should give output of 1 for 300ms, if it doesn't recieve any other input that is 1 within 300ms, it would set the output to 0 but if it received and input of 1 within 300ms, it would reset the timer and give output as 1 for next 300ms. I have never worked with timer based programming in c++. so I can't find any ideas about how to do it. If anyone can provide me an answer or point me in right direction, I would be really grateful. Thank you very much for time replying to my query.
-
Good morning everyone. I am a newbie in c++ coding and recently encountered a problem related to timer. Note: i am with MFC, so I can use the timer from MFC. but not sure how to about it. what I have to do is looks something like this; if (input = 1) { start timer for 300ms ; output = 1 ; } if ( new input within 300ms = 1) { restart the timer for 300ms; output = 1; } if (input != 1 after 300ms || time elasped > 300ms ) { output = 0; } I have a a stream of input 0 and 1 coming from an array. requirement is that after the program received an input of 1 from the array, it should give output of 1 for 300ms, if it doesn't recieve any other input that is 1 within 300ms, it would set the output to 0 but if it received and input of 1 within 300ms, it would reset the timer and give output as 1 for next 300ms. I have never worked with timer based programming in c++. so I can't find any ideas about how to do it. If anyone can provide me an answer or point me in right direction, I would be really grateful. Thank you very much for time replying to my query.
crucial1953 wrote:
I have never worked with timer based programming in c++
Timers are not part of the native C++ language. You either have to invent your own, or use some third party library that contains one. On a semi-related note, the
clock()
function should help to get you started, at last in knowing how many (milli)seconds have elapsed between two points in time."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Good morning everyone. I am a newbie in c++ coding and recently encountered a problem related to timer. Note: i am with MFC, so I can use the timer from MFC. but not sure how to about it. what I have to do is looks something like this; if (input = 1) { start timer for 300ms ; output = 1 ; } if ( new input within 300ms = 1) { restart the timer for 300ms; output = 1; } if (input != 1 after 300ms || time elasped > 300ms ) { output = 0; } I have a a stream of input 0 and 1 coming from an array. requirement is that after the program received an input of 1 from the array, it should give output of 1 for 300ms, if it doesn't recieve any other input that is 1 within 300ms, it would set the output to 0 but if it received and input of 1 within 300ms, it would reset the timer and give output as 1 for next 300ms. I have never worked with timer based programming in c++. so I can't find any ideas about how to do it. If anyone can provide me an answer or point me in right direction, I would be really grateful. Thank you very much for time replying to my query.
To do it by polling which you sort of imply with your pseudocode (It is better with interrupts if possible to trigger the 1 event). You define an ID for the timer and a timer sample count
#define IDT_TIMER_0 WM_USER + 100
UINT sampleCount = 0;
You set the timer interval
SetTimer(IDT_TIMER_0, 10, NULL);//10ms poll x 30 samples = 300ms
You do the checks in on_timer call back function and reset the sample count if you find what you wanted
void OnTimer (UINT TimerVal)
{
sampleCount++;
if (sample_whatever == 1)
{
// output your 1
sampleCount = 0;
} else if (sampleCount == 30)
{
// output your 0
sampleCount = 0;
}
}You can use KillTimer with the id to kill the timer at any time you like
KillTimer(IDT_TIMER_0);
There are more advanced techniques like using WaitForSingleObject but we need to know more about what the 1 is coming from.
In vino veritas