Go Down

Topic: Intervall within an intervall (wihout delay) (Read 294 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 ]

dehein2


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy