When using delay() to flash a LED there is a time for the LED to be on and then off. This makes it easy to have independent control of the “on” and “off” times. The standard blink without delay example doesn’t give you this flexibility.

millis-cookbook-two-duty-cycles
This example code gives you complete independent control of how long a LED (or any OUTPUT pin) stays “ON” or “OFF”. This also demonstrates a very simple two-state state machine.
The variable “LED13state” is used to track what should happen each time the millis() event fires.
// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;
// Tracks the last time event fired
unsigned long previousMillis=0;
// Interval is how long we wait
int interval = onTime;
// Used to track if LED should be on or off
boolean LED13state = true;
// Usual Setup Stuff
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
// Set Pin 13 to state of LED13state each timethrough loop()
// If LED13State hasn't changed, neither will the pin
digitalWrite(13, LED13state);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
// Change wait interval, based on current LED state
if (LED13state) {
// LED is currently on, set time to stay off
interval = offTime;
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
// Toggle the LED's state, Fancy, eh!?
LED13state = !(LED13state);
// Save the current time to compare "later"
previousMillis = currentMillis;
}
}

