Go Down

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

#### dehein2

##### Feb 06, 2013, 11:41 pm
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

#1
##### Feb 06, 2013, 11:56 pm
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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

#2
works perfectly.
Thanks a lot

Go Up