Blink without delay doubt [Solved]

Hi there,

I have a doubt, I went through the blink without delay sketch and realized that the LED only blinks according to the set interval, that is if the interval was 1000 millis then the code will do the same as what the delay function below does.

digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);

My question is that what if I wanted 2 different intervals for HIGH and LOW then how will the code look like?. Example- what if LOW interval was 1000 millis but HIGH interval was only 10 millis like mentioned below?

digitalWrite(13, HIGH);
delay(10);
digitalWrite(13, LOW);
delay(1000);

If this was the case how can we do that in blink without delay?

.

Noobian:
If this was the case how can we do that in blink without delay?

Finite state machine. In your example there are two states.

Make an attempt. Post it. Invite others to critique. Learn.

HIGH and LOW states?

And now you know why I dislike that particular example. It sucks.

Take a gander at this thread: Timing trouble with millis() for LED control - Programming Questions - Arduino Forum

In it I (and several other people) help the OP learn how to code a lighting sequence for multiple LEDs, including a button to switch between different sequences. It can be stripped down to just flash one LED with different on/off times.

Noobian:
HIGH and LOW states?

...are reasonable names for the two states. Anything that helps the future you / other people understand what you are trying to accomplish is a good choice.

If this was the case how can we do that in blink without delay?

When you change the LED state change the value of the period variable.

I found this code and it seems to work.

const unsigned int onTime = 10;
const unsigned int offTime = 1000;
unsigned long previousMillis=0;
int interval = onTime;
boolean LED13state = true;

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

digitalWrite(13, LED13state);
 

unsigned long currentMillis = millis();
 
 
if ((unsigned long)(currentMillis - previousMillis) >= interval){
 if (LED13state) {
 interval = offTime;
 } 
 else  {
 interval = onTime;
 }
 LED13state = !(LED13state);
 previousMillis = currentMillis;
 }
}

Lots of ways to do what you want.

Try this

const byte ledPin =  13;
unsigned long previousMillis;
const byte states[] = {HIGH, LOW};
const unsigned long intervals[] = {10, 1000};
byte state;

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

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % 2])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[state++ % 2]);
  }
}

or for extra fun, try this

const byte ledPin =  13;
unsigned long previousMillis = 0;
const byte states[] = {HIGH, LOW};
const unsigned long intervals[] = {200, 200, 200, 800};
byte state;

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

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % 4])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[state++ % 2]);
  }
}

UKHeliBob:
Lots of ways to do what you want.

Try this

Awesome... Thanks man.

I just had to change this part

const unsigned long intervals[] = {10, 1000};

to

const unsigned long intervals[] = {1000, 10};

May all of your blinks be without delay().. indeed :slight_smile:

I just had to change this part

const unsigned long intervals[] = {10, 1000};

The order of the period data needed depends on how the LEDs are wired so you did the right thing for your circuit.