Re: How do I do something every x increment in time?

From: CaptainJ (jeffseacrest_at_spamjam.bresnan.net)
Date: 03/30/05


Date: Tue, 29 Mar 2005 21:56:51 -0700


"wwwolf" <abuse@alltel.net> wrote in message
news:c6587$4249f2b3$a6664320$13830@ALLTEL.NET...
> How do I write code that executes a statement every 0.25 seconds while
> other
> conditions are still being evaluated? (I hope I got the terminology right)
>
> Psuedocode:
> 1. Set up a timer to display a message 5 seconds from now.
> 2. While 1 is activated, "cout << "." << flush;" once every 0.25 seconds
> until 1
> is ready to print its message.
>
> I understand I will need 2 time variables. 1 will be "timer" and 2 will be
> "dotDelay". I think I will need to evaluate dotDelay and any dotDelay
> statements while it is placed inside the "timer while block" since this
> block
> needs to keep control. Or, dotDelay needs to be given control to execute
> its
> statement every 0.25 seconds and then pass control back to the timer loop.
>
> I've noticed that loops will 'delay' the next statement outside of their
> block
> loop until the loop returns false. At first I tried to set up a while loop
> inside of another while loop to 'delay' the "." message. It did exactly
> what I
> told it to (not what I wanted) by delaying the "." until x time and then
> continue to blast the screen with the "." message until timer was ready to
> cout.
>
> 1. What is the terminology called for the block that executes a statement
> every
> x increment in time?
>
> 2. Is this where a function would be needed?
>
> Could someone show me an example of how to set this up?
> Thanx again for your patience.
>
> // Example for 5 second timer w/ delay dot
>
> #include <iostream>
> #include <ctime>
>
>
> int main()
> {
> using std::cin;
> using std::cout;
> using std::flush;
>
> time_t timer = ((time (0)) +5); // Add 5 sec to current time
>
> while( timer >= time (0) ) // Compare the timer w/ current time
> {
> cout << "." << flush; // Would like this to only execute every 0.25
> seconds
> }
> cout << "\nFive seconds have passed.\n" << flush;
> return 0;
> }
>
> If I have gotten any terminology wrong then plaease correct me. One of the
> best
> tools for learning is being able to ask the right questions.
>
> Thanx again,

This works for a one second delay, but I'm not familiar with the time()
function so this is just a structural idea:

#include <iostream>
#include <ctime>

int main()
{
   using std::cin;
   using std::cout;
   using std::flush;

   time_t timer = ((time (0)) + 5); // Add 5 sec to current time
   time_t increment = ((time(0)) + 1);
   time_t base = time(0);

   while( timer >= time (0) ) // Compare the timer w/ current time
      {
      if(base >= increment)
            {
            cout << "." << flush; // Would like this to only execute every
0.25seconds
            increment = ((time(0)) + 1);
            }
      base = time(0);
      }
   cout << "\nFive seconds have passed.\n" << flush;
   return 0;
}



Relevant Pages

  • How do I do something every x increment in time?
    ... Set up a timer to display a message 5 seconds from now. ... statement every 0.25 seconds and then pass control back to the timer loop. ... What is the terminology called for the block that executes a statement every ...
    (alt.comp.lang.learn.c-cpp)
  • Re: PLL Terminology Question
    ... IMO this terminology is used only in Gardner's book; ... It is about P, PI, or PII control loop. ... It certainly has universal meaning in control systems terms: ...
    (comp.dsp)
  • Re: Polling, Interrupts, DMA, Synchronous, Asynchronous I/O Definitions
    ... the terminology is less useful than it might be. ... though a "message loop" could arguably be claimed to be ... considering in a particular iteration but what is true for _ALL_ ... watching the "polling" version eating up every single CPU cycle ...
    (alt.lang.asm)
  • Re: Simple Count down Timer Project! New post!
    ... This particular board runs the laser right around room temperature, ... usually don't have to have a big delta-T in order to get good control. ... it was that putting the control loop into the digital domain meant ... ADC and PWM for free with a $2 micro, ...
    (sci.electronics.design)
  • Re: Peltier elements
    ... appropriately slow auxiliary loop, while gaining the fast response of a ... You do need to monitor the temperature of the exhaust ... the information to add a bit of feed-forward control as well, ... How do you partition the feedback between the slower Peltier junction ...
    (sci.electronics.design)