Led does not turn on each minute for five seconds

Hi!

I just started learning Arduino a couple of weeks ago and so far, the delay function worked splendid, however now I want to do multiple tasks without stopping the program.

I would like to turn on a led for five seconds each minute, I used the "BlinkWithoutDelay" sketch and set the time for one minute, but I do not know how to order the Arduino to turn on the LED just for five seconds each minute.

I would appreciate your help!

int LED = 13;
unsigned long previousMillis = 0;     
const long interval = 60000; // 1 min           

void setup() 
{
  pinMode(LED,OUTPUT);
}

void loop() 
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) //Do this each 1 min
  {
    previousMillis = currentMillis;
    
    digitalWrite(LED,HIGH); // Turn on LED each minute for 5 seconds
  }
}

When you turn the led on , record millis again “next millis” into a variable , and have a “second interval” ( 5 secs) , after millis has passed the value of “ next millis”, plus “second interval”, turn the led off .

lami874674:
Hi!

I just started learning Arduino a couple of weeks ago and so far, the delay function worked splendid, however now I want to do multiple tasks without stopping the program.

I would like to turn on a led for five seconds each minute, I used the "BlinkWithoutDelay" sketch and set the time for one minute, but I do not know how to order the Arduino to turn on the LED just for five seconds each minute.

I would appreciate your help!

something like this would probably do the trick:

int LED = 13;
unsigned long previousMillis;

//************assuming period is 1 min***************
const long ON_time = 5000; // 5sec
const long OFF_time = 55000; // 55sec

void setup()
{
  pinMode(LED, OUTPUT);
  previousMillis = millis();
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > OFF_time)
  {
    previousMillis = currentMillis;

    digitalWrite(LED, HIGH); // Turn on LED
  }
  else if (currentMillis - previousMillis > ON_time && digitalRead(LED) == HIGH) {
    previousMillis = currentMillis;

    digitalWrite(LED, LOW); // Turn off LED
  }
}
const byte ledPin =  13;
unsigned long previousMillis = 0;
const byte states[] = {HIGH, LOW};
const unsigned long intervals[] = {5000, 55000};  //on, off periods
const byte NO_OF_INTERVALS = sizeof(intervals) / sizeof(intervals[0]);
byte state;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % NO_OF_INTERVALS])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[state++ % 2]);
  }
}

Yet another way: :slight_smile:

void setup()
{
  pinMode(LED_BUILTIN,OUTPUT);
}
void loop()
{
  static unsigned long startTime = millis();
  unsigned long interval = 60000UL, onTime = 5000;
  digitalWrite(LED_BUILTIN,millis() - startTime < onTime);
  if(millis() - startTime > interval)
    startTime += interval;
}

Thank you! this one helped me to do what I wanted

HOORAY for the author of this one. :slight_smile: