How to blink multiple LED?

How can I blink multiple LEDs WITHOUT using delay() function in Arduino? I tried to use millis() functions but I have trouble managing timestamps. What is the easiest way to do it?

Answer

Managing timestamps when blinking multiple LEDs is not easy for newbies. Fortunately, the ezOutput library supports blinking multiple LED. The timestamp is managed by the library. Therefore, we can use this library without managing timestamps. Further more, you can use an array of LEDs to make code clean and short

Array of LEDs

When you use many LEDs, you can use an array of LEDs to make code clean and short. The below example code use an array for 5 LEDs.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/faq/how-to-blink-multiple-led */ #include <ezOutput.h> const int LED_NUM = 5; const int LED_1_PIN = 2; const int LED_2_PIN = 3; const int LED_3_PIN = 4; const int LED_4_PIN = 5; const int LED_5_PIN = 6; ezOutput ledArray[] = { ezOutput(LED_1_PIN), ezOutput(LED_2_PIN), ezOutput(LED_3_PIN), ezOutput(LED_4_PIN), ezOutput(LED_5_PIN) }; void setup() { Serial.begin(9600); for (byte i = 0; i < LED_NUM; i++) { ledArray[i].blink(500, 250); // 500 milliseconds ON, 250 milliseconds OFF } } void loop() { for (byte i = 0; i < LED_NUM; i++) ledArray[i].loop(); // MUST call the loop() function first }

See Also

The Best Arduino Starter Kit

※ OUR MESSAGES