View Single Post
  #2   Spotlight this post!  
Unread 09-05-2011, 17:39
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Wait(); function

Here's some simple timer code that you can apply to this situation as well.

Code:
Timer* t;
bool running = false;
bool driveState = false;
...
// In constructor
t = new Timer();
t->Reset();
...
// In periodic function
int motorSpeed = 0.0;
bool triggerPressed = Thirdstick.GetTrigger();

// When the trigger is pressed, the desired behavior is
// 1. Run the motor forward at half speed for 1 second.
// 2. After one second has passed, turn the motor off
// 3. When another second has passed, return to #1
if(triggerPressed == true)
{
    if(running == false)
    {
        // We aren't running yet.  Start the timer and set the flag
        t->Start();
        running = true;
        driveState = true;
    }

    if(t->HasPeriodPassed(1.0))
    {
        // The one second timer has passed.  Change the drive state
        driveState = !driveState;
        // Reset the timer
        t->Reset();
    }

    // Select a motor speed based on the drive state
    if(driveState == true)
    {
        motorSpeed = 0.5;    
    }
    else
    {
        motorSpeed = 0.0;
    }
}
else
{
    // The trigger is not pressed.  

    // Stop it and reset it
    t->Stop();
    t->Reset();

    // Clear the global variables so that they're ready to
    // go when the button is pressed again
    running = false;
    driveState = false;
}

// Set the motor output
motor->Set(motorSpeed);
Reply With Quote