BlinkWithoutDelay longer light up time for LED before blinking

How do i increase the time for the LED when lit before it blinks again and without increasing the interval for LED light off? This is the code I got: (from https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay )

/* 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
 modified 11 Nov 2013
 by Scott Fitzgerald


 This example code is in the public domain.

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

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

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // 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);
  }
}
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
      interval = 2000;
    }
    else
    {
      ledState = LOW;
      interval = 1000;
    }

UKHeliBob:

    // if the LED is off turn it on and vice-versa:

if (ledState == LOW)
    {
      ledState = HIGH;
      interval = 2000;
    }
    else
    {
      ledState = LOW;
      interval = 1000;
    }

it didn't work. it instead just inceased the off time of the blinking LED.

joshuaevangelista:
it didn't work. it instead just inceased the off time of the blinking LED.

So, swap the "interval" values over.

AWOL:
So, swap the "interval" values over.

did that already. i also had it to be in interval = 5000 with the other to be 1000 and vice versa. all it just do is increased the off time and not the on time. the on time is the same for all values of intervals in both conditions as i have observed.

Post your code

Here is the code:
``const int ledPin = 13; // the number of the LED pin
int ledState = LOW;
unsigned long previousMillis = 0;
int interval = 1000;

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

void loop() {

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;

if (ledState == LOW) {
ledState = HIGH;
interval = 5000;
//interval = 1000;
} else {
ledState = LOW;
interval = 1000;
//interval = 5000;
}
digitalWrite(ledPin, ledState);
}
}

(Uncompiled, untested, inappropriate values)

const byte ledPin =  13;      // the number of the LED pin

byte ledState;             // ledState used to set the LED
unsigned long previousMillis;        // will store last time LED was updated

const unsigned long interval  = 1000;           // interval at which to blink (milliseconds)
const unsigned long offTime   = 950;
const unsigned long onTime    = interval - offTime;
void setup() 
{
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) 
  {
    previousMillis += (ledState) ? offTime : onTime;   

    ledState = ! ledState;

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

My code here is my experiment with that a year and a bit ago. It uses the idea explained above where you just set the on or off interval each time.

It does it for two LEDs independently. It puts the blinking code in functions, which has no fundamental effect but does make things a little tidier.

As always, E&OE, YMMV....

/* Blink without Delay on steroids
 
 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.
 
 This enhanced version firstly does that for 2x LEDs not just 1, but also
 allows the on and off times to differ.
 
 LEDs named 1 and 2 in the appropriate code lines below.
 
 For simplicity the work is done in functions.
 
 The circuit:
 * LEDs attached from pin 12 and 14 to ground, don't forget the resistors
 

 
 Based on the original, 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This mod by JimboZA, December 2014
 
 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 ledPin1 =  12;      // the number of the LED pin
const int ledPin2 = 14;      // the number of the LED pin

// Variables will change:
int ledState1 = LOW;             // ledState used to set the LED
long previousMillis1 = 0;        // will store last time LED was updated
int ledState2 = LOW;             // ledState used to set the LED
long previousMillis2 = 0;        // will store last time LED was updated

// for the pulse led....
// 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;           // interval at which to blink (milliseconds)
long onTime1 = 1000;
long offTime1 = 5000;

long interval2;           // interval at which to blink (milliseconds)
long onTime2 = 110;
long offTime2 = 8530;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);  
  //Serial.begin(9600);    
}

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


void pulseLED1()
{
// 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.
  //Serial.println("in pulseLED");
  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;
      // set the interval to how long it must stay on
      interval1=onTime1;
    }
    else
    {
      ledState1 = LOW;
      // set the interval to how long it must stay off
      interval1=offTime1;
    }

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


void pulseLED2()
{
// 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.
  //Serial.println("in pulseLED");
  unsigned long currentMillis = millis();
 
  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;
      // set the interval to how long it must stay on
      interval2=onTime2;
    }
    else
    {
      ledState2 = LOW;
      // set the interval to how long it must stay off
      interval2=offTime2;
    }

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

Your code in #6 (long on, short off) works as expected for me by the way, so now I have no idea what the question is. If that's not what you want, then you'll need to explain it better.

it didn't work. it instead just inceased the off time of the blinking LED.

There have been many replies since you made this comment so I hope that you are now sorted, but I think that if you had looked more than just briefly at the code I posted then you might have seen how it worked and how the values of interval could be changed to do what you want.

One of the best things about the Arduino is that it is easy to try things out. I still remember that the first Arduino program that I loaded and ran was the Blink sketch, not even BWOD, and the first thing that I changed was the value of the delay to see what happened.

Don't be afraid to try things.

An example using C++

class BLed {
    byte pin;
  public:
    unsigned int onTime;
    unsigned int offTime;
    unsigned long lastChange;
    BLed(byte iPin, unsigned int iOn = 500, int iOff = 500) { // set by default
      pin = iPin;
      onTime = iOn;
      offTime = iOff;
    }
    void begin() {
      pinMode(pin, OUTPUT);
    }
    void process(unsigned long currentMillis) {
      if (digitalRead(pin)) {
        if (currentMillis - lastChange >= onTime) {
          lastChange += onTime;
          digitalWrite(pin, LOW);
        }
      } else {
        if (currentMillis - lastChange >= offTime) {
          lastChange += offTime;
          digitalWrite(pin, HIGH);
        }
      }
    }
};

BLed Builtin(LED_BUILTIN, 100, 900);  // set from constructor

void setup() {
  Builtin.begin();
  Builtin.offTime = 1900; // set from code
}
void loop() {
  unsigned long topLoop = millis();
  Builtin.process(topLoop);
}

Try this simple sketch from a simple mind, adjust onTime and offTime to suit.

uint32_t  start;
const uint16_t onTime = 1500, offTime = 1000;
const byte ledPin = 13;

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

void loop() {
  if(millis() - start < onTime)
    digitalWrite(ledPin,HIGH);
    else digitalWrite(ledPin,LOW);
  
  if(millis() - start > onTime + offTime)
    start = millis();
}

I prefer the solution in reply #1. It's the most straightforward and easy to understand.

aarg:
I prefer the solution in reply #1. It's the most straightforward and easy to understand.

But OP said that didn't work for him I think, although OP's code at #6 works for me.

JimboZA:
But OP said that didn't work for him I think, although OP's code at #6 works for me.

He appears to have ignored (or been unable to grasp?) a simple fix. So I don't have much sympathy. Really, it isn't that hard to figure out that if the times are reversed, all you have to do is swap them (or change the polarity). There isn't much hope for a programmer that can't see that.

aarg:
There isn't much hope for a programmer that can't see that.

So probably the wrong time to introduce my bwod library to him then.....

I prefer the solution in reply #1. It's the most straightforward and easy to understand.

But my simpleton code is 78 bytes shorter. :grin:

outsider:
But my simpleton code is 78 bytes shorter. :grin:

Not after compilation. :slight_smile: Or is it?

You want short? Beat this:

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

void loop()
{
  digitalWrite(LED_BUILTIN, !(millis() & 0x600));
}

... but it does glitch once every 49 days...