Go Down

Topic: Intervall within an intervall (wihout delay) (Read 256 times) previous topic - next topic

dehein2

Hi,
i want something to happen every x seconds for y seconds. So i tried this:

Code: [Select]

void PressTrigger(){

if((millis()-TriggerTimer)>(Delay*1000)){
 digitalWrite(Trigger, HIGH);
 delay(500);
 digitalWrite(Trigger, LOW);
 TriggerTimer = millis();
}
}


So the first level works fine. The if is executed every x seconds. Every x seconds i want the Trigger to be HIGH for y seconds (here 500ms). But how can i do this within the first condition without using delay?

Thanks
Dennis

MarkT

If you use a variable to represent the current state (two possible values), then use a switch statement to determine the
current state in the loop(), something a bit like:
Code: [Select]

int state ;

void setup ()
{
  state = 0 ;
  TriggerTimer = millis () ;
  // plus other initialization code.
}

void loop ()
{
  switch (state)
  {
  case 0:
    if (millis() - TriggerTimer > xDelay * 1000)
    {
      digitalWrite (Trigger, HIGH) ;
      TriggerTimer = millis () ;
      state = 1 ;
    }
    break ;
  case 1:
    if (millis() - TriggerTimer > yDelay * 1000)
    {
      digitalWrite (Trigger, LOW) ;
      TriggerTimer = millis () ;
      state = 0 ;
    }
    break ;
  }
  // plus any other stuff ....
}
[ I won't respond to messages, use the forum please ]


Go Up