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