Skip to content

Instantly share code, notes, and snippets.

@justinledwards
Last active March 7, 2023 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinledwards/8139051 to your computer and use it in GitHub Desktop.
Save justinledwards/8139051 to your computer and use it in GitHub Desktop.
8 LED blink without delay
/* 8 LEDs blinking at random intervals without delay
Using the millis method you don't have to wait for an operation to happen before executing the next bit of code.
If you don't use this method you can't overlap state changes.
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
const int ledPin2 = 12;
const int ledPin3 = 11;
const int ledPin4 = 10;
const int ledPin5 = 9;
const int ledPin6 = 8;
const int ledPin7 = 7;
const int ledPin8 = 6;
const int buzzPin = 2;
// Variables will change:
int ledState = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
int ledState4 = LOW;
int ledState5 = LOW;
int ledState6 = LOW;
int ledState7 = LOW;
int ledState8 = LOW;
int buzzState = LOW;// ledState used to set the LED
long previousMillis = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;
long previousMillis5 = 0;
long previousMillis6 = 0;
long previousMillis7 = 0;
long previousMillis8 = 0;
long previousBuzzMillis = 0;// will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10000;
long interval2 = 9000;
long interval3 = 8000;
long interval4 = 7000;
long interval5 = 6000;
long interval6 = 5000;
long interval7 = 4000;
long interval8 = 3000;
long intervalBuzz = 150;// interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(buzzPin, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// 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.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > random(200, interval)) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > random(200, interval2)) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);
}
unsigned long currentMillis3 = millis();
if(currentMillis3 - previousMillis3 > random(200, interval3)) {
// save the last time you blinked the LED
previousMillis3 = currentMillis3;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin3, ledState3);
}
unsigned long currentMillis4 = millis();
if(currentMillis4 - previousMillis4 > random(200, interval4)) {
// save the last time you blinked the LED
previousMillis4 = currentMillis4;
// if the LED is off turn it on and vice-versa:
if (ledState4 == LOW)
ledState4 = HIGH;
else
ledState4 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin4, ledState4);
}
unsigned long currentMillis5 = millis();
if(currentMillis5 - previousMillis5 > random(200, interval5)) {
// save the last time you blinked the LED
previousMillis5 = currentMillis5;
// if the LED is off turn it on and vice-versa:
if (ledState5 == LOW)
ledState5 = HIGH;
else
ledState5 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin5, ledState5);
}
unsigned long currentMillis6 = millis();
if(currentMillis6 - previousMillis6 > random(200, interval6)) {
// save the last time you blinked the LED
previousMillis6 = currentMillis6;
// if the LED is off turn it on and vice-versa:
if (ledState6 == LOW)
ledState6 = HIGH;
else
ledState6 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin6, ledState6);
}
unsigned long currentMillis7 = millis();
if(currentMillis7 - previousMillis7 > random(200, interval7)) {
// save the last time you blinked the LED
previousMillis7 = currentMillis7;
// if the LED is off turn it on and vice-versa:
if (ledState7 == LOW)
ledState7 = HIGH;
else
ledState7 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin7, ledState7);
}
unsigned long currentMillis8 = millis();
if(currentMillis8 - previousMillis8 > random(200, interval8)) {
// save the last time you blinked the LED
previousMillis8 = currentMillis8;
// if the LED is off turn it on and vice-versa:
if (ledState8 == LOW)
ledState8 = HIGH;
else
ledState8 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin8, ledState8);
}
unsigned long currentBuzzMillis = millis();
if(currentBuzzMillis - previousBuzzMillis > intervalBuzz) {
// save the last time you blinked the LED
previousBuzzMillis = currentBuzzMillis;
// if the LED is off turn it on and vice-versa:
if (buzzState == LOW) {
buzzState = HIGH;
intervalBuzz = 10000;
}
else {
buzzState = LOW;
intervalBuzz = 150;
}
// set the LED with the ledState of the variable:
digitalWrite(buzzPin, buzzState);
}
}
@lepczynski
Copy link

lepczynski commented May 27, 2019

Maybe it'd be worth putting those variable sets of 8 (previous millis and intervals) into arrays and then have a loop instead of copying the same code 8 times?
Cheers!

Copy link

ghost commented Nov 19, 2019

I'm new to Arduino and am trying to figure out if I could use this code for my project. I only need one LED to blink, so I can get rid of the duplicate sections of code. The question I have is about this section
long interval = 3000;
long intervalBuzz = 150; // interval at which to blink (milliseconds)
Do these variables allow me to set the delay between each blink and the length of the blink separately? In other words, can I make the time the LED is on and the time it is off different?

@drf5n
Copy link

drf5n commented Mar 7, 2023

interval is used for the LED on pin 13, and intervalBuzz is used for the buzzer on pin 2. If you want different lengths of on time and off time the buzz section would work:

  unsigned long currentBuzzMillis = millis(); 
  if(currentBuzzMillis - previousBuzzMillis > intervalBuzz) {
    // save the last time you blinked the LED 
    previousBuzzMillis = currentBuzzMillis;   

    // if the LED is off turn it on and vice-versa:
    if (buzzState == LOW) {
      buzzState = HIGH;
      intervalBuzz = 10000;
    }
    else {
      buzzState = LOW;
      intervalBuzz = 150;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(buzzPin, buzzState);
  }

Note that you could replace all of the unsigned long currentBuzzMillis = millis(); with just one unsigned long currentMillis = millis(); and re-use the currentMillis in all of the tests like:

  if(currentMillis - previousBuzzMillis > intervalBuzz) {
    // save the last time you blinked the LED 
    previousBuzzMillis = currentMillis;   
    ...

or even:

  if(currentMillis - previousBuzzMillis > intervalBuzz) {
    // save the last time you blinked the LED 
    previousBuzzMillis += intervalBuzz;   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment