Blink LED Using Millis()

Hi! I'm working on a project using blink without delay, specifically millis().
I need to blink 5 LEDs at various rates for lengths of time each.
I've got parts of my code working...

The first section of code works fine:

Blink all 5 LEDs at 1 blink per second

//goal: continuously flash all 5 at 1/sec
unsigned long timeNow = millis(); //find time elapsed since program started
if(timeNow - lastTime > int1) //if current time - last time > interval you want to blink at
{ lastTime = timeNow; //update lastTime to be now, as the last time the LED blinked
if(ledState == LOW){ //if the LEDstate is 0
ledState = HIGH; //make it 1
}
else{ //if the LEDstate is 1
ledState = LOW; //make it 0
}
}
digitalWrite(LED1, ledState); //update LED1's state to 1 or 0
digitalWrite(LED2, ledState);
digitalWrite(LED3, ledState);
digitalWrite(LED4, ledState);
digitalWrite(LED5, ledState);
}

I'm having trouble with a specific section:
Blink LED1 and LED 5 1 time per second for 2 seconds, then LED2 and 4 1 time per second for 2 seconds, then LED3 1 time per sec for 2 seconds.

I've got it working using delay(), however the idea of this project is to use blink without delay.

digitalWrite(LED1, HIGH); digitalWrite(LED5, HIGH); delay(1000);
digitalWrite(LED1, LOW); digitalWrite(LED5, LOW);delay(1000);
digitalWrite(LED1, HIGH); digitalWrite(LED5, HIGH);delay(1000);
digitalWrite(LED1, LOW); digitalWrite(LED5, LOW);delay(1000);
digitalWrite(LED2, HIGH); digitalWrite(LED4, HIGH);delay(1000);
digitalWrite(LED2, LOW); digitalWrite(LED4, LOW);delay(1000);
digitalWrite(LED2, HIGH); digitalWrite(LED4, HIGH);delay(1000);
digitalWrite(LED2, LOW); digitalWrite(LED4, LOW);delay(1000);
digitalWrite(LED3, HIGH); delay(1000);
digitalWrite(LED3, LOW); delay(1000);
digitalWrite(LED3, HIGH); delay(1000);
digitalWrite(LED3, LOW); delay(1000);
digitalWrite(LED2, HIGH); digitalWrite(LED4, HIGH);delay(1000);
digitalWrite(LED2, LOW); digitalWrite(LED4, LOW);delay(1000);
digitalWrite(LED2, HIGH); digitalWrite(LED4, HIGH);delay(1000);
digitalWrite(LED2, LOW); digitalWrite(LED4, LOW);delay(1000);}

How can I use millis() to make this work?

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Some code to experiment with

unsigned long startTimes[] = {0, 0, 0, 0};  //start time of current blink for each LED
unsigned long periods[] = {500, 510, 520, 530};    //period for each LED
const byte ledPins[] = {3, 5, 6, 9};  //the LED pins
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);  //calculate the number of LEDs

void setup()
{
  Serial.begin(115200);
  Serial.print("Number of LEDs : ");
  Serial.println(numberOfLeds);
  for (int pin = 0; pin < numberOfLeds; pin++)  //use the number of LEDs to set pinMode()s
  {
    pinMode(ledPins[pin], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis(); //get current time for all timing in loop()
  for (int led = 0; led < numberOfLeds; led++)  //for each LED
  {
    if (currentTime - startTimes[led] > periods[led]) //if the period has ended
    {
      digitalWrite(ledPins[led], !digitalRead(ledPins[led])); //change the state of the LED
      startTimes[led] = currentTime; //save the start time of the blink for the current LED
    } //end of test for period ended
  } //end of for loop
} //end of loop()

All of your delay intervals are identical. If it will always be like that, you could just use the same timing framework as BlinkWithoutDelay but instead of toggling an LED, use each timed event to move through the states of a state machine. Each state corresponds to one LED change.

Well, if you want to just get this done (using libraries) give a shout.

-jim lee

Using libraries, he doesn't learn anything!

Datman:
Using libraries, he doesn’t learn anything!

Would you therefore advocate not using digitalWrite(), digitalRead(), analogWrite(), analogRead() etc, etc, etc ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.