Flashing multiple LEDs at the same time

I think the ideas in this thread are worthy of a new example in the IDE, per

Here's my effort:

/*
  BlinkWithoutDelayMore -- Blink without Delay for more LEDs, using Functions

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  State diagram: tinyurl.com/BWODMdiag 

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi
  modified by 7 Mar 2023
  by David Forrest

  This example code is in the public domain.

  For a tutorial on how this works, please read:
  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay

  Simulation: https://wokwi.com/projects/358554786853847041

  This was written for the discussion at
  https://forum.arduino.cc/t/the-fact-that-something-has-been-used-for-a-long-time-is-in-no-way-an-argument-for-making-it-the-same-for-another-100-years/1097951/24
  on improving the BlinkWithoutDelay example.

  For other ways of doing multiple things at once please read:
  https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564/
  https://learn.adafruit.com/multi-tasking-the-arduino-part-1/now-for-two-at-once


*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;  // the number of the LED pin
const int ledPin2 = 3; // the number of the second LED pin

// Variables will change:
int ledState = LOW;  // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 1000;  // interval at which to blink (milliseconds)

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

void loop() {
  // here is where you'd put code that needs to be running all the time.

  checkIfItIsTimeToBlinkAndDoIt();  
  // ^^ this function separates the BlinkWithoutDelay core
  // into a function

  checkIfItIsTimeToBlinkAndDoIt_variable(500);  // function handles a separate LED
}

/////// 

void checkIfItIsTimeToBlinkAndDoIt() {
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.  

  // save the current time
  unsigned long currentMillis = millis();

  // test if it is time yet
  if (currentMillis - previousMillis >= interval) {
    // save the last time you changed the LED
    previousMillis = currentMillis;

    // Toggle the ledState variable:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED output to match the ledState variable:
    digitalWrite(ledPin, ledState);
  } // end interval check

}

void checkIfItIsTimeToBlinkAndDoIt_variable(unsigned long interval) {
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  // If it is not yet time it immediately returns control back to loop()

  // These "local scope" variables mask variables of the same name in other scopes
  // this helps keep the global namespace cleaner and easier to maintain
  const int ledPin = ledPin2 ; // copy from global constant
  // use static for private state variables that will persist across calls
  static bool initialized = false;
  static int ledState = LOW;
  static unsigned long previousMillis ;

  // save the current time
  unsigned long currentMillis = millis();

  if (initialized == false) {  // 
    ledState = LOW;
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, ledState);
    initialized = true;
    previousMillis = millis();
  }

  // test if it is time yet
  if (currentMillis - previousMillis >= interval) {
    // save the last time you changed the LED
    previousMillis = currentMillis;

    // Toggle the ledState variable:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED output to match the ledState variable:
    digitalWrite(ledPin, ledState);
  } // end interval check

}

(I reserve the right to edit this code to follow the sim in that Wokwi link. )

In contrast with the other example, my schematic follows BWOD by using LED_BUILTIN on 13, while the other is on pin 3.

What I like about my code is that it builds directly from the venerable BWOD, giving a clear path forward from doing one thing without delay to DoingSeveralThingsAtTheSameTime without delay. I think it would be good to highlight this thread, and the other good tutorials on how to solve the same problem in the code's comments, because comparing different ways to do the same thing helps with learning.

Also pointing at more advanced multitasking tutorials would be helpful:

2 Likes