Blink without delay with independent on/off times?

I'm just wondering if anyone has seen an example of Blink without delay with independent on/off times. (ex. On for 1000ms, off for 500ms) It's easy to do with the the standard blink example but I plan on using a LCD screen to display other info so I can't have a delay. It seems that the standard blink without delay examples I'm seeing have the on/off times locked together so you can only use one time span for on/off.

this might be what I was looking for, a 1ms delay is fine with me.

void setup() {
   pinMode(13, OUTPUT)
}
 
void loop() {
   digitalWrite(13, HIGH);   // set the LED on
   for (int x=0; x < 1000; x++) {     // Wait for 1 second
      delay(1);
   }
   digitalWrite(13, LOW);   // set the LED on
   for (int x=0; x <; 1000; x++) {     // Wait for 1 second
      delay(1);
   }
}

Using a 1 milliSecond delay 1000 times is no better than using a 1000 millisecond delay once.

How about

unsigned long timeLedChanged = millis();
unsigned long period = 1000;
boolean ledOn = true;
const byte ledPin = 13;

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

void loop() 
{
  if (millis() - timeLedChanged >= period)
  {
    ledOn = !ledOn;
    timeLedChanged = millis(); 
    digitalWrite(ledPin, ledOn);  
    if (ledOn)
    {
      period = 1000;
    }
    else
    {
      period = 500; 
    }
  }
}

or perhaps

unsigned long timeLedChanged = millis();
unsigned long period = 1000;
boolean ledOn = true;
const byte ledPin = 13;
const int periods[] = {500, 1000};

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

void loop() 
{
  if (millis() - timeLedChanged >= period)
  {
    ledOn = !ledOn;
    timeLedChanged = millis(); 
    digitalWrite(ledPin, ledOn);  
    period = periods[ledOn];
  }
}

compubob:

void loop() {

digitalWrite(13, HIGH);   // set the LED on
  for (int x=0; x < 1000; x++) {     // Wait for 1 second
     delay(1);
  }
  digitalWrite(13, LOW);   // set the LED on
  for (int x=0; x < 1000; x++) {     // Wait for 1 second
     delay(1);
  }
}

Hey I recognize that code… (http://www.baldengineer.com/blog/2011/01/06/millis-tutorial/) Example #2? Not exaclty how I intended for it to be used.

Go to example #4. Add an “if-statement” to determine if the LED is currently on or off. Then increment the waitUntil based differnently for the on and off times.

unsigned long onTime = 1000;
unsigned long offTime = 500;
unsigned long waitUntil=0;
unsigned long interval = onTime;
boolean LED13state = true;

void setup() {
  pinMode(13, OUTPUT);
}
void loop() {
  // Each time through loop(), set Pin 13 to the same value as the state variable
  digitalWrite(13, LED13state);
  // Nothing will change until millis() increments by 1000

  if ((unsigned long)(millis() - waitUntil) >= interval) {
    if (LED13state) {
      // LED is currently on
      interval = offTime;
    }  else {
      // LED is currently off
      interval = onTime;
    }
    LED13state = !(LED13state);        // Toggle the LED's state
    waitUntil += interval;   
  }
}

UKHeliBob: Using a 1 milliSecond delay 1000 times is no better than using a 1000 millisecond delay once.

How about

unsigned long timeLedChanged = millis();
unsigned long period = 1000;
boolean ledOn = true;
const byte ledPin = 13;

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

void loop() {  if (millis() - timeLedChanged >= period)  {    ledOn = !ledOn;    timeLedChanged = millis();    digitalWrite(ledPin, ledOn);      if (ledOn)    {      period = 1000;    }    else    {      period = 500;    }  } }

THANK YOU! I took this code and modified it to work with 4 potentiometers, 2 for seperate LED dimming and 2 for ON time and OFF time, I changed it a lot, but it worked well from this base code. And the LCD screen is updating the values from the pots in real time without any delays.

you were right, the first code I posted was still causing a delay that was causing the lcd screen to update slower as the ON or OFF time was adjusted higher.

ha!, yah I just happened to find it right after I posted this, but it didn’t work the way I needed. So this is off your page? well thanks for the help anyways :slight_smile:

compubob: So this is off your page? well thanks for the help anyways :)

Yeah, the code posted has a typo in it, so I recognized it...

yah, I noticed it too when I tried to run it.