 # Help, Generate a recurring double pulse using the milis() function

Hi I am trying to generate a recurring double pulse using the milis() function.

I can generate a pulse... code below. But I would like my wave form for have two pulse separated by a longer period.

for example pulse one and two to be HIGH for 100ms and LOW for 100ms after the two pulses has occurred then there should be a LOW for 1000ms followed by the pulses again. can someone help me with the code.

thanks

void BlinkLEDOnMode(const long interval) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; }

// set the LED with the ledState of the variable: digitalWrite(LED, ledState);

} }

You need 2 different `previousMillis`, one per pulse

For what you want, you need to change the value of the variable interval for the different stages. It needs to be 100, 100, 100, 1000 (HIGH, LOW, HIGH, LOW). Your code is

``````if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
``````

and after that add the lines

``````if (step < 3) {
interval = 100;
}
else {
interval = 1000;
}
step ++;
if (step > 3) {
step = 0;
}
``````

And it would probably be better to change previousMillis = currentMillis; to previousMillis += interval; for greater accuracy (see Several Things at a Time for an explanation.

…R

Can you clarify if the pulses are on the same pin or on two different pins?

Edit: re reading your post, it's same pin most likely, so yes Robin's approach would work

J-M-L: You need 2 different `previousMillis`, one per pulse

No, actually one millis variable is enough - you need to use 4 states, one per transition - the same previousMillis can be reused for each segment of time, as they are necessarily sequential. Something like:

``````  if (state == InFirstPulse && millis () - previousMillis > FIRST_PULSE_DURATION)
{
state = BetweenPulses ;
previousMillis = millis () ;
digitalWrite (LED, LOW) ;
}
else if  (state == BetweenPulses && millis () - previousMillis > INTER_PULSE_GAP)
{
state = InSecondPulse ;
previousMillis = millis () ;
digitalWrite (LED, HIGH) ;
}
...
``````

If you run several timed tasks in parallel, then you need a variable per task.

Yep - as mentioned above, I misread his intent, thought he wanted to drive 2 pins at different pace

thanks everyone. I tried the if statement and it worked out nicely. I may need to tweak this to have lower instructions per task.

``````if (step < 3) {
interval = 100;
}
else {
interval = 1000;
}
step ++;
if (step > 3) {
step = 0;
}
``````

Moderator edit: [code][/code] tags added. (Nick Gammon)