millis() One pin. Two different delays Help?

Hi
Does Anyone have an example of using Millis() with two delays on the same pin.

I would like a pin to high for 10 sec and low for 2 sec. delay statement won't work for my application.
Any examples that work will be useful I would just like to see how it would be done .

Thanks
Matt

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 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 interval_high = 10000;           // interval at which to blink (milliseconds)
long interval_low  = 2000;           // interval at which to blink (milliseconds)
int   flag                = 0;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
if ( flag ) {
  if(currentMillis - previousMillis > interval_high) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    ledState = LOW;
    flag = 0;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
else
{
  if(currentMillis - previousMillis > interval_low) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    ledState = HIGH;
    flag = 1;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

}
}

Not Tested !

Huh!!! I knew it was that easy I was so close. It works beautifully. Im not sure about the flag statement. Could you tell me how that works?

Thanks
Matt

There are many ways to do it. Here is one I just wrote and tested:

// pin 4 high 10 sec, low 2 sec. Tom Fangrow, Aug 6, 2012

unsigned long oldTime=0, period1=10000, period2=2000;

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

void loop() {
  if(millis() - oldTime < period1) {
    digitalWrite(4, HIGH);
  }
  else if(millis() - oldTime < period1 + period2) {
    digitalWrite(4, LOW);
  }
  else {
    oldTime = millis();
  }
}

Since 'flag' and 'ledState' track each other there is no need to have both. You can also reduce some of the redundant code.

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
boolean ledState = LOW;             // ledState used to set the LED
long previousMillis = 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 interval_high = 10000;           // interval at which to blink (milliseconds)
long interval_low  = 2000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > (ledState ? interval_high : interval_low)) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    ledState = !ledState;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

I didn't test my version either. :slight_smile:

Im not sure about the flag statement. Could you tell me how that works?

Frankly, as a programmer I'm "hobbyist". What I did is "replicate" electronics device "flip flop". Btw, basically they do same thing in PLC programming, just replicate relay circuitry behavior.

Edited: just read new post arrived, I agree, flag isn't strictly necessary in this example. But for multiple state system flag and switch/case statement is probably, most elegant solution

Wow! I appreciate all the help. Didn't realize there are so many ways to do it. Google was not my friend on this one. I have been trying for days to figure it out on my own.