Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. updating output value with timer and resetting if the timer exceeds a certain time or it has a new input value

updating output value with timer and resetting if the timer exceeds a certain time or it has a new input value

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasedata-structureshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crucial1953
    wrote on last edited by
    #1

    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.

    D L 2 Replies Last reply
    0
    • C crucial1953

      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.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C crucial1953

        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.

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups