Pages: [1]   Go Down
Author Topic: Intervall within an intervall (wihout delay)  (Read 225 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 ....
}
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

works perfectly.
Thanks a lot
Logged

Pages: [1]   Go Up
Jump to: