Blink without delay independent offtime control

After reviewing the blink without delay example I see that you can easily make an led turn on for 3 seconds then it will turn off for 3 seconds, but i want an led to turn on for 3 seconds and turn off for 5 seconds.

How can I control the off time of an led without using delay?

const int ledPin =  8;      // 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 = 3000;           // 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 > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

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

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

interval is a variable, so you can vary it! When you switch LED state set interval to the appropriate delay.

I got the change in variable... which changes to both on and off time to be the same amount.

but how can I control the OFFTIME independent of the ONTIME. I need 3 seconds on and 5 off.

basically i need to change the puse width and duty cycle. Using the "blink without delay "example it keeps the pulse width & Duty cycle the exact same at 50% of your total cycle. I need to make my pulse width & Duty Cycle adjustable so I need to control the time that the led is OFF witch I cannot figure out how to do.

Change interval when you change the led state.

const unsigned long ONTIME = 3000UL;
const unsigned long OFFTIME = 5000UL;

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 > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      interval = ONTIME;
      ledState = HIGH;
    }
    else
    {
     interval = OFFTIME; 
     ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

lg8215:
basically i need to change the puse width and duty cycle. Using the "blink without delay "example it keeps the pulse width & Duty cycle the exact same at 50% of your total cycle. I need to make my pulse width & Duty Cycle adjustable so I need to control the time that the led is OFF witch I cannot figure out how to do.

Blink Without Delay is an example. It obviously won't solve every problem that consists of an LED turning on and off. So you really need to do more than just compile and run it. You need to understand WHAT it does and HOW it does what it does.

The principle is what's important. Once you know the principle, you will never need to load and run it again.

Here's the principle.

I do someting, and note the time I did it. I know how long I should wait until I do something else. So every once in a while, I check to see what time it is, subtract the time I noted, and if enough time has elapsed, I do the next thing.

It really doesn't matter if the first thing was to turn an LED on, or if the second thing was to start up a motor. You just need to keep track of time and check for duration. It's. just. that. simple.

Jimmy60:
Change interval when you change the led state.

That makes perfect sense! Change the the variable interval as it changes states! I was stuck on where to put the change for the OFFTIME. Thank you so much! Prob. Solved! Now I can apply this to the rest of my code!

Blink Without Delay is an example. It obviously won't solve every problem that consists of an LED turning on and off. So you really need to do more than just compile and run it. You need to understand WHAT it does and HOW it does what it does.

Agreed, I actually have a much more complicated program running I just used delays for the pulses, then I started reading about how bad they were and decided to swtich my delays for micros() and got stuck so I went back to the most basic example and worked from there!

It really doesn't matter if the first thing was to turn an LED on, or if the second thing was to start up a motor. You just need to keep track of time and check for duration. It's. just. that. simple.

my "LED" is actually a fuel injector and is why i needed to control the duty cycle and Pulse Width.

Thanks all for your help!

There is a mistake in Blink Without Delay.

long previousMillis = 0;        // will store last time LED was updated

This will cause an error because millis() is an unsigned long, which is 32 bits, integer, and only positive. This would normally roll over every 49.7 days, and previousMillis is also an unsigned long, it does NOT cause an error because of the way 2's complement math works. Or so they tell me. If you use micros(), also an unsigned long, the rollover is only about 72 minutes. In both cases, and unsigned long is 2^32 - 1 counts or about 4.3 thousand million counts. 4.3 million seconds for millis(), 4.3 thousand seconds for micros().

http://arduino.cc/en/Reference/Micros#.UwVNrPldWeQ

http://arduino.cc/en/Reference/UnsignedLong#.UwVN2fldWeQ

But with previousMillis merely a long, a long is both positive and negative and so one bit of the 32 is to indicate the sign, so after 2^31 - 1 counts, the program no longer works correctly. That's about 2.15 thousand million counts, which is 2.15 million seconds for millis(), and 2.15 thousand seconds for micros(). Or about 24 days for millis(), and about 36 minutes for micros().

http://arduino.cc/en/Reference/Long#.UwVN9fldWeQ

So merely changing this to make it an unsigned long fixes this. The only issue is if you are using millis() and want something to take longer than 49 days, or using micros() and want it to take longer than 72 minutes. In that case, simply adding one variable that counts the number of times either micros() or millis() rolls over can take care of that.

unsigned long previousMillis = 0;        // will store last time LED was updated

In fact, later in the program, the variable currentMillis is created as an unsigned long.

This error is also in the example sketch "Debounce". In that case, two variables are created as long that should be unsigned long.

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

Should be:

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

I hope this is of some help.