Project tutorial
Walk Away From Delay

Walk Away From Delay © MIT

Using millis in Arduino to time things.

  • 2,675 views
  • 6 comments
  • 11 respects

Components and supplies

About this project

This video was inspired from questions I sow on-line. on how to blink 2 LED in different rates.

If we wanted to blink a led every 1000 millis and the second one every 500 millis, we could still use delay, like this, as you can see I split the 1000 millis delay of the first LED into 500 millis blink of the second dealy.

byte LED2 = 12; 
void setup() {
 pinMode(LED_BUILTIN, OUTPUT);
 pinMode(LED2, OUTPUT);
}
void loop() {
 digitalWrite(LED_BUILTIN, HIGH);   
 digitalWrite(LED2, HIGH);   
 delay(500);                       
 digitalWrite(LED2, LOW);   
 delay(500);                 
 digitalWrite(LED_BUILTIN, LOW);    
 digitalWrite(LED2, HIGH);   
 delay(500);                       
 digitalWrite(LED2, LOW);   
 delay(500);                 
}

But what will happen if we wanted to blink the faster one in 300 millis instead of the 500 millis.Here is where the use of delay makes it very complicated to not possible.

So what to do? Stop using delay!

We even got an example for it in the Arduino IDE and its called blinkwithoutdelay.

I duplicate the logic of the first LED to a second one and set the desired delay.

So try it as well, and walk away from delay.I will say one finale thing, the code I shared is far from being perfect, and I thought of changing it but, since this video is aimed at people that are new to arduino, I voted against it.

Code

Example Code Arduino
const int ledPin =  LED_BUILTIN;
int ledState = LOW;             
unsigned long previousMillis = 0; 
const long interval = 1000;           

const int ledPin2 =  12;
int ledState2 = LOW;             
unsigned long previousMillis2 = 0; 
const long interval2 = 300;     

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  } //end if 

  if (currentMillis - previousMillis2 >= interval2) {
    previousMillis2 = cur rentMillis;
    if (ledState2 == LOW) {
      ledState2 = HIGH;
    } else {
      ledState2 = LOW;
    }
    digitalWrite(ledPin2, ledState2);
  } //end if 

  
}

Schematics

Schematics
Fritzing bb eir69q2ltr

Comments

Similar projects you might like

2-Way Intersection with Pedestrian Walk Cycle

Project showcase by FalcomDigital

  • 4,363 views
  • 2 comments
  • 10 respects

How to Make an IR Sensor

Project tutorial by Manikant savadatti

  • 8,704 views
  • 0 comments
  • 17 respects

Telegram Bot with ESP8266

Project tutorial by Shebin Jose Jacob

  • 38,821 views
  • 44 comments
  • 75 respects

Measure your reaction time

Project tutorial by Jayraj Desai

  • 32,622 views
  • 9 comments
  • 34 respects

Ultrasonic Range Finder

Project tutorial by Saaket Poray

  • 14,950 views
  • 10 comments
  • 16 respects

Joystick Controller for MeArm Robot - Recording Coordinates

Project tutorial by utilstudio

  • 9,838 views
  • 6 comments
  • 33 respects
Add projectSign up / Login