Fun with Millis(): Blink two LEDs

In this series I want to show short snippets of code around millis().

Blink two LEDs

The "Blink Without Delay" examples shows how to blink one LED based on milllis().

But can we blink two LEDs? Yes we can!

/*
  Fun with Blink without Delay

  Turns on and off two light emitting diode (LED) connected to a digital pin
  vice versa.

  https://forum.arduino.cc/t/two-leds-using-millis/901634/
*/

const int ledAPin = 13;               // the number of the LED pin
const int ledBPin = 2;                // another LED
char ledState = 'A';                  // ledState used to set the LED
unsigned long previousMillis = 0;     // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode(ledAPin, OUTPUT);
  pinMode(ledBPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    // if the LED A is on turn it off and vice-versa:
    if (ledState == 'A') {
      ledState = 'B'; // switch to state B for the next iteration
      digitalWrite(ledAPin, LOW);
      digitalWrite(ledBPin, HIGH);
    } else {
      ledState = 'A';
      digitalWrite(ledAPin, HIGH);
      digitalWrite(ledBPin, LOW);
    }
  }
}

The variable ledState has a new type char. It stores A if LED A is on, or B if LED B is on.

Blink Three LEDs

What does it need to add a third LED? I think it's obvious: you need a third LED and a new state

/*
  Fun with Blink without Delay

  Turns on and off three light emitting diode (LED) connected to a digital pin
  vice versa.

  https://forum.arduino.cc/t/two-leds-using-millis/901634/
*/

const int ledAPin = 13;               // the number of the LED pin
const int ledBPin = 2;                // another LED
const int ledCPin = 3;                // 
char ledState = 'A';                  // ledState used to set the LED
unsigned long previousMillis = 0;     // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode(ledAPin, OUTPUT);
  pinMode(ledBPin, OUTPUT);
  pinMode(ledCPin, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    // if the LED A is on turn it off and vice-versa:
    if (ledState == 'A') {
      ledState = 'B';
      digitalWrite(ledAPin, LOW);
      digitalWrite(ledBPin, HIGH);
      digitalWrite(ledCPin, LOW);
    } else if (ledState == 'B') {
      ledState = 'C';
      digitalWrite(ledAPin, LOW);
      digitalWrite(ledBPin, LOW);
      digitalWrite(ledCPin, HIGH);
    } else if (ledState == 'C') {
      ledState = 'A';
      digitalWrite(ledAPin, LOW);
      digitalWrite(ledBPin, LOW);
      digitalWrite(ledCPin, HIGH);
    }
  }
}

We just added a new pin, an additional state C (for the third LED) and some lines of code.

A Larson Scanner / KITT Scanner

As we are already have 3 blinking LEDs you might ask, if we can expand this to a Larson Scanner like seen on the famous KITT. Yes for sure, Just follow the Link at the end of the page.

Links

(*) Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you I will earn a (little) comission if you click through and make a purchase. I only recommend products I own myself and I'm convinced they are useful for other makers.

History

First upload: 2021-09-02 | Version: 2024-03-22