You probably dont want to have any kind of nested loops in your main program loop, unless you are certain it will execute fast enough. A for loop will block the execution of the code after it, and will fire the watch dog, or worse, cause your bot to "freeze" its output values while the loop is executing.
Instead, you could use a Timer object.
Code:
class MyRobot : public IterativeRobot
{
// Declare the timer
Timer * kickTimer;
// Then down in the constructor...
MyRobot(void){
// ...
kickTimer = new Timer();
}
void TeleopPeriodic(void){
if( somethingHappened ){
kickTimer->Start();
mySolenoid->Set(true);
}
if( kickTimer->Get() >= 1.0){
mySolenoid->Set(false);
kickTimer->Stop();
kickTimer->Reset();
}
}
Or you could just increment a counter every time you loop around
Code:
static unsigned int solenoidCounts = 0;
static bool kickerFired = false;
if(somethingHappened){
solenoid->Set(true);
kickerFired = true;
solenoidCounts = 0;
}
if(kickerFired){
solenoidCounts++;
if(solenoidCounts > 1 * 40){ // 40Hz(ish)
solenoid->Set(false);
kickerFired = false;
solenoidCounts = 0; // just to be safe
}
}
It's not the prettiest thing in the world, but it gets the job done and its pretty easy to understand/implement/change.