## Introduction: Blink Multiple Leds at Different Rates, 1 Function, No Delay

In this Instructables we will go step-by-step from the standard BlinkWithoutDelay sketch to a single function that we can recall for every leds we have.

Required:

• Arduino Board
• Arduino IDE installed
• at least 2 Leds
• 2 Current Limiting Resistors

Connect the hardware like in the picture and open the Arduino IDE.

## Step 1: From BlinkWithoutDelay to a Single Function

Here is a short example of the standard sketch that use millis() instead of Delay().

```#define blueLed 3

unsigned long previousMillis = 0;        // stores last time Led blinked

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

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

}

void loop() {
if (millis() - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = millis();

}

}```

And this is how we can compress it in a function, with some limitation yet.

```#define blueLed 3

void setup() {
pinMode(blueLed, OUTPUT); //pin3 Output
}
void loop() {

}
static long prevMill=0; //prevMill stores last time Led blinked
if (((long)millis() - prevMill) >= interval){
prevMill= millis(); //stores current value of millis()
}```

With this function we dont need to declare any variables except the led pin and they can have different interval but we can't use it for more than one led. Every led must have his own function, pretty annoying:

```#define blueLed 3
#define greenLed 2

void setup() {
pinMode(blueLed, OUTPUT); //pin3 Output
pinMode(greenLed, OUTPUT); //pin2 Output
}

void loop() {
}
static long prevMill = 0;
if (((long)millis() - prevMill) >= interval){
prevMill = millis();
}
}
static long prevMill = 0;
if ((millis() - prevMill) >= interval){
prevMill = millis();
}
}```

In the next step we'll try to write a single function that can be used with multiple leds.

## Step 2: A Single Function for Multiple Leds

Finally this is the code for a single function to control multiple Leds.

The previousMillis variable has been sostituited with an array that stores all the previousMillis for each led.

When you call the function you have to insert 3 arguments: led (pin or variable), interval (in ms), array (must be different for each led).

This way every led can have is own interval, minimal variables required (only the array) and Delay is never used.

```BLINK FUNCTION FOR MULTIPLE LED

#define blueLed 3   //
#define greenLed 2  //pin for each led

unsigned long previousMillis[2]; //[x] = number of leds

void setup() {
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop() {
BlinkLed(blueLed, 100, 0);   //BlinkLed( which led, interval, one of the stored prevMillis
BlinkLed(greenLed, 200, 1);  //last parameters must be different for each led
}

void BlinkLed (int led, int interval, int array){

//(long) can be omitted if you dont plan to blink led for very long time I think
if (((long)millis() - previousMillis[array]) >= interval){

previousMillis[array]= millis(); //stores the millis value in the selected array