# Blink without delay for 2 different time intervals

I am looking to use the basic blink without delay program but instead of having the the light blink on and off for only one period of time, I would like to keep the light on for one period and off for another. I need to keep with millis() instead of any delay functions because I need to monitor inputs while this is going on. Is someone able to show me some basic code that would accomplish this?

Try the Multi_Blink example in the Playground.

Looking at Multi-blink, I get lost in a hurry. I could probably work it out but, it is not a beginner sketch imho. http://arduino.cc/playground/Code/MultiBlink

How about blinking 5 leds at individual rates without delay! The sketch could be made smaller but, it should be easy to see a pattern.

``````/* Blink without Delay

*/

// constants won't change. Used here to set pin numbers:
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
const int led1 =  13;      // the number of the LED pin
const int led2 =  5;       // the number of the second LED
const int led3 =  6;       //3rd
const int led4 =  7;       //4th
const int led5 =  8;       //5th
// Variables will change:
int ledState1 = LOW;             // ledState used to set the LED
int ledState2 = LOW;             // ledState used to set the LED
int ledState3 = LOW;             // ledState used to set the LED
int ledState4 = LOW;             // ledState used to set the LED
int ledState5 = LOW;             // ledState used to set the LED
long previousMillis1 = 0;        // will store last time LED was updated
long previousMillis2 = 0;        // will store last time LED was updated
long previousMillis3 = 0;        // will store last time LED was updated
long previousMillis4 = 0;        // will store last time LED was updated
long previousMillis5 = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval1 = 500;           // interval at which to blink (milliseconds)
long interval2 = 700;           // interval at which to blink (milliseconds)
long interval3 = 800;           // interval at which to blink (milliseconds)
long interval4 = 400;           // interval at which to blink (milliseconds)
long interval5 = 550;           // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop()
{

unsigned long currentMillis = millis();

if(currentMillis - previousMillis1 > interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis;

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

// set the LED with the ledState of the variable:
digitalWrite(led1, ledState1);
}
if(currentMillis - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis;

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

// set the LED with the ledState of the variable:
digitalWrite(led2, ledState2);
}
if(currentMillis - previousMillis3 > interval3) {
// save the last time you blinked the LED
previousMillis3 = currentMillis;

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

// set the LED with the ledState of the variable:
digitalWrite(led3, ledState3);
}
if(currentMillis - previousMillis4 > interval4) {
// save the last time you blinked the LED
previousMillis4 = currentMillis;

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

// set the LED with the ledState of the variable:
digitalWrite(led4, ledState4);
}
if(currentMillis - previousMillis5 > interval5) {
// save the last time you blinked the LED
previousMillis5 = currentMillis;

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

// set the LED with the ledState of the variable:
digitalWrite(led5, ledState5);
}
}
``````

I think the OP just wants to blink one LED, but with something other than a 50% duty cycle. I use code similar to this:

``````#define LED_PIN 13
#define LED_ON 800       //milliseconds
#define LED_OFF 200

unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
boolean ledState;        //current LED state

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

void loop(void)
{
ms = millis();
}

{
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}
``````

I think the OP just wants to blink one LED, but with something other than a 50% duty cycle. I use code similar to this:

Sorry, after reading the post again, I agree. Darn and it took me a bit of time to write that sketch too! lol

No thanks very much for this code. I want to blink LED's like a police car. For fun and to learn. Such as headlights that go back and forth and red and blue lights that blink fast and then even a set of strobes that blink fast as well, but not as fast as the red and blue's but faster than the headlights. If you wanted to go full meal deal then another set that strobe as weill in a pattern (the ambers)

I am new to the arduino but I hope to dissect your code and learn what it is doing. I find I learn faster when I can look at something and make changes and see what happens. Such as it doesn't work anymore or the rates change.

THANKS!!

Andrew

``````// <http://arduino.cc/forum/index.php/topic,111702.msg903903.html#new>

#include <WProgram.h>

//const uint8_t         pinLED      = 13;   // Arduino Uno
//const uint8_t         pinLED      = 13;   // Arduino Mega
//const uint8_t         pinLED      = 13;   // Digilent chipKIT Uno32
//const uint8_t         pinLED      = 77;   // Digilent chipKIT Max32
//const uint8_t         pinLED      = 54;   // Digilent Cerebot MX7 ck

const unsigned long     dtON_DELAY  = 500UL;
const unsigned long     dtOFF_DELAY = 250UL;

const uint8_t           LED_OFF     = LOW;
const uint8_t           LED_ON      = HIGH;

uint8_t                 stateLED    = LED_OFF;
unsigned long           dtTrigger;

bool hasTimeArrived(const unsigned long& dtTrigger)
{
return (((long)(millis() - dtTrigger)) >= 0);
}

{
stateLED = ((stateLED == LED_ON) ? LED_OFF : LED_ON);
digitalWrite(pinLED, stateLED);

return ((stateLED == LED_ON) ? dtON_DELAY : dtOFF_DELAY);
}

void loop()
{
if ( hasTimeArrived(dtTrigger) )
{
}
}

void setup()
{
dtTrigger = millis();

pinMode(pinLED, OUTPUT);
}
``````

turn on led i%4 turn off led (i-1)%4 //% = modulo ..4 leds // i a counter

you can use my timer library, very simple to use. no delays, small footprint.

after you import the library, check the sample and test it.

``````/*
Title: Simple Switch Timer
Library: sstimer.ino
Description:
Created Date: January 17, 2015
Created By: Rolly Falco Villacacan
Released into the public domain.
*/

#include <sstimer.h>

//WATER_PUMP SETUP
int PIN_NO1 = 13;
long unsigned OFF_DURATION = 500; //in milliseconds HOW LONG YOU WANT THE PIN TO BE OFF STATE
long unsigned ON_DURATION = 500; //in milliseconds //HOW LONG YOU WANT THE PIN TO BE ON STATE

//LIGHTHING SETUP
int PIN_NO2 = 12;
long unsigned OFF_DURATION2 = 1000; //in milliseconds
long unsigned ON_DURATION2 = 1000; //in milliseconds

sstimer WATER_PUMP(PIN_NO1, ON_DURATION, OFF_DURATION); //create instance for water pump
sstimer LIGHTHING(PIN_NO2, ON_DURATION2, OFF_DURATION2);//create instance for lighting

void setup(){
//...
}

void loop(){
//...
WATER_PUMP.startsstimer();
LIGHTHING.startsstimer();
//..
}
``````

/

``````void loop(){
//...
WATER_PUMP.startsstimer();
LIGHTHING.startsstimer();
//..
}
``````

Start the timers each time trough loop() Is that right ?

I want to blink LED's like a police car. For fun and to learn. Such as headlights that go back and forth and red and blue lights that blink fast and then even a set of strobes that blink fast as well, but not as fast as the red and blue's but faster than the headlights. If you wanted to go full meal deal then another set that strobe as weill in a pattern (the ambers)

Have a look at Several things at the same time

;D opppsss

I got your point now, sorry I think i'ved name the function wrong...i should have not use the work "start" if should be any name, and i admit it could cause confusion now.

I'll can change it to "check"

thanks man, :)

done,

``````void loop(){
//...
WATER_PUMP.check();
LIGHTHING.check();
//..
}
``````

:D