Thread: sleep()
View Single Post
  #14   Spotlight this post!  
Unread 22-02-2004, 20:00
kristen's Avatar
kristen kristen is offline
PI loves you!
#0639 (Code Red)
Team Role: Human Player
 
Join Date: Jan 2003
Location: Ithaca, NY
Posts: 283
kristen will become famous soon enough
Send a message via AIM to kristen
Re: sleep()

Ok, you'll need two global variables (put them in user_routines_fast.c, or something):
Code:
Code:
unsigned int delay_count = 0;
unsigned int delay_constant = 0;
enum {PROGRAM_RUNNING, PROGRAM_DELAY} program_state = PROGRAM_RUNNING;
Have a function:
Code:
Code:
void delay(unsigned int count)
{
    if (!delay_constant) {
        delay_constant = count;
        delay_count = 0;
        program_state = PROGRAM_DELAY;
    }
    if (delay_count >= delay_constant) {
        delay_constant = delay_count = 0;
        program_state = PROGRAM_RUNNING;
    } else {
        delay_count += 26;
    }
}
This function should be called something like this: (in user_routines_fast.c)
Code:
Code:
 Getdata(&rxdata);   /* DO NOT DELETE, or you will be stuck here forever! */

         if (PROGRAM_RUNNING == program_state) {
             /* Add your own autonomous code here. */
         } else {
             delay(0);
         }

        Putdata(&txdata);   /* DO NOT DELETE, or you will get no PWM outputs! */
    }
Now, anytime you want to set a delay, simply have a call to delay(milliseconds) somewhere in the autonomous code.

Most of this might not compile.

.. A certain programmer on my team who is unnamed is tooo shy to post this himself! ;D

Last edited by kristen : 22-02-2004 at 20:03.