HELP with blink without delay..

I am familiar with the code blink without delay..
here is the code

# define led  13
int ledstate = 0;
long previousmillis = 0;

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

void loop(){
  unsigned long currentmillis = millis();
  
  if(currentmillis - previousmillis >= 1000){
    digitalWrite(led, ledstate);
    ledstate = !ledstate;
    previousmillis = currentmillis;
  }
}

with blink without delay only I would like to blink an led for 30milisecond at every 1 second interval.
like in this code below..

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

void loop(){
  digitalWrite(13, 1);
  delay(30);
  digitalWrite(13, 0);
  delay(1000);
}

Just cannot make out how to do it ..

Just cannot make out how to do it ..

Here's a hint. When the pin is on, interval (a variable in the original sketch; hardcoded in your version) has one value. When the pin is off, interval needs to have another value.

Just cannot make out how to do it ..

Yes you can, think;

You note the start time and turn the LED on.
30ms after the start you turn the LED off.
1030ms after the start time you repeat the process.

What you see is;

0ms turn on.
30ms turn off.
1030ms turn on.
1060ms turn off.
2060ms turn on.
:
:

Here's two ways:
#1 starts with LED on

const byte led = 13;

long previousmillis = 0;

void setup(){
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
}

void loop(){
  unsigned long currentmillis = millis();

  if(currentmillis - previousmillis >= 30){
    digitalWrite(led, LOW);
  }
  if(currentmillis - previousmillis >= 1030){
    digitalWrite(led, HIGH);
    previousmillis = currentmillis;
  }
}

#2 Starts with LED off

const byte led = 13;

long previousmillis = 0;

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

void loop(){
  unsigned long currentmillis = millis();

  if(currentmillis - previousmillis >= 1000){
    digitalWrite(led, HIGH);
  }
  if(currentmillis - previousmillis >= 1030){
    digitalWrite(led, LOW);
    previousmillis = currentmillis;
  }
}

Here's two ways

It's far simpler to set interval to one value when you turn the pin on, and to a different value when you turn the pin off.

PaulS:
It's far simpler to set interval to one value when you turn the pin on, and to a different value when you turn the pin off.

Not quite understanding what should be simple.
Example please?

Not quite understanding what should be simple.

If your LED is off, set "interval" to be your "on" time and turn the LED on.
If your LED is on, set "interval" to be your "off" time and turn the LED off.

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

int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

long interval = 1000;           // interval at which to blink (milliseconds)

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

void loop()
{
  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;
       interval = 30;
    }
    else
    {
       ledState = LOW;
       interval = 1000;
    }

    digitalWrite(ledPin, ledState);
  }
}

When the pin is to be turned on, assign interval one value. When it is to be turned off, assign it another value. There is nothing in the example or philosophy that says that interval needs to be a constant.

Ok. Thanks alot, got it now.
Sometimes it's easier if I see an example

PaulS:

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

int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

long interval = 1000;           // interval at which to blink (milliseconds)

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

void loop()
{
  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;
       interval = 30;
    }
    else
    {
       ledState = LOW;
       interval = 1000;
    }

digitalWrite(ledPin, ledState);
  }
}




When the pin is to be turned on, assign interval one value. When it is to be turned off, assign it another value. There is nothing in the example or philosophy that says that interval needs to be a constant.

Thanks a lot Pauls.. :slight_smile:

Now I have a problem..:frowning:
The main reason why I wanted blink without delay..

I am showing you the code with the delay .

void setup(){
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
}

void loop(){
  
  if(digitalRead(2) == LOW){
    digitalWrite(13, HIGH);
    delay(20);
    digitalWrite(13, LOW);
    delay(20);
  }
  
  else{
    digitalWrite(13, HIGH);
    delay(30);
    digitalWrite(13, LOW);
    delay(1000);
  }
}

In the code above I want the led to blink rapidly when PIN 2 is LOW.. else blink for 30 sec at 1 sec interval..

The problem comes in here that when the led just glew and went off and I make PIN2 LOW.. The led does not start to blink rapidly instantly..
It waits for the delay of 1 second to complete and then starts the rapid blinking if() loop..:frowning:

How do I eliminate this..??

How do I eliminate this..??

By setting the time that the LED last changed state, and setting the interval to the appropriate value.

That is all that should happen as a result of the switch being pressed. The toggling of the LED should be independent of the switch state.

PaulS:

How do I eliminate this..??

By setting the time that the LED last changed state, and setting the interval to the appropriate value.

That is all that should happen as a result of the switch being pressed. The toggling of the LED should be independent of the switch state.

I am not getting you .:frowning:

Can you plz show me with an example..?