Flashing multiple LEDs at the same time

C style code
My code below flashes 2 LEDs, one connected to pin 2 and one to Pin 3, each with a suitable resistor (220 Ohms or thereabouts) to 0V. Each LED flashes independently of the other.

Notes
I have used separate functions called from the loop function to illustrate putting code into functions rather than trying to do everything in the loop function.

There are 2 different flash functions, each uses millis() independently for timing. The first, flashEqualOnOffPin2(), flashes the LED on pin 2 with equal on and off times. The second, flashUnequalOnOffPin3(), flashes the LED on pin 3 and allows the on and off times to be different.

Having 2 functions illustrates having non-blocking code called from the loop function. At no point does any part of any function wait for anything, it just checks to see if it is time to change the state of a pin and if it is then it changes the pin then returns, otherwise it just returns having done nothing.

To flash more LEDs copy the relevant flash function, change its name and the pin number, paste it into your code and call it from the loop function.

There is a lot that can be done to improve this code, such as having an array for the pin numbers, having an array for the on and off times for each pin, having one function for flashing everything and passing the required pin number, on and off times to it. Each of these improvements would be worth exploring but each would also complicate the code. My aim was to make the code simple for beginners.

Tested on a Nano Every, should work on most if not all Arduino boards.

If the comments in the code do not adequately explain it then please ask.

Complete sketch

    const int LED_PIN2 = 2;
    const int LED_PIN3 = 3;

    #define LED_ON HIGH
    #define LED_OFF LOW
    
void setup() {
    Serial.begin(115200);
    pinMode(LED_PIN2, OUTPUT);
    pinMode(LED_PIN3, OUTPUT);
    digitalWrite(LED_PIN2, LED_OFF);         // Give the output pins a definite, known state at start up
    digitalWrite(LED_PIN3, LED_OFF);
    Serial.println("Setup complete");
}

void loop() {
    flashEqualOnOffPin2();
    flashUnequalOnOffPin3();
}

void flashEqualOnOffPin2() {
    uint32_t currentMillis = millis();
    static uint32_t lastMillis;
    const uint32_t onOffTime = 500;                 //Duration for LED to be on or off before changing state, in miliseconds
    
    if (currentMillis - lastMillis >= onOffTime) {
        lastMillis += onOffTime;


        
    if (digitalRead(LED_PIN2) == LED_ON) {
            digitalWrite(LED_PIN2, LED_OFF);
        } else {
            digitalWrite(LED_PIN2, LED_ON);
        }
    }
}

void flashUnequalOnOffPin3() {
    uint32_t currentMillis = millis();
    static uint32_t lastMillis;
    const uint32_t onTime = 1750;     //Duration for LED to be on, in miliseconds
    const uint32_t offTime = 350;     //Duration for LED to be off, in miliseconds

        if (digitalRead(LED_PIN3) == LED_ON) {                      // Read the current state of pin 3
            if (currentMillis - lastMillis >= onTime) {             // Pin 3 is high (LED on), check on time and turn LED off if time has expired
                lastMillis += onTime;                               // Add the value of onTime to lastMillis ready to start the next timing interval
                digitalWrite(LED_PIN3, LED_OFF);                    // Turn LED off
            }
     } else {
        if (currentMillis - lastMillis >= offTime) {                // Pin 3 is low (LED off), check off time and turn LED on if time has expired
            lastMillis += offTime;                                  // Add the value of offTime to lastMillis ready to start the next timing interval
            digitalWrite(LED_PIN3, LED_ON);                         // Turn LED on
        }
     }
}

Simulation

Edit: Code modified for clarity, with thanks to @camsysca for suggested improvements.

8 Likes