Search

Walk Away From Delay

Photo of talofer99

Made by talofer99

About the project

Using millis in Arduino to time things.

Project info

Difficulty: Easy

Platforms: Arduino

Estimated time: 1 hour

License: MIT license (MIT)

Items used in this project

Hardware components

Arduino Uno - R3 Arduino Uno - R3 x 1
LED - Basic Red 3mm LED - Basic Red 3mm x 2

Software apps and online services

ARDUINO IDE ARDUINO IDE

Story

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.

Schematics and circuit diagrams

Schematics

Code

Example Code

Credits

Photo of talofer99

talofer99

Maker @ heart

   

Leave your feedback...