Tutorial Understanding Blink Without Delay | How to millis

Introduction

In this tutorial, we will learn how BlinkWithoutDelay example can be use to do multiple task at the same time.

Circuit Diagram

Video Demonstration

Source Code

 1
 2// LED definitions for easy reference
 3#define RED_LED   A0
 4#define YEL_LED   A1
 5#define BLU_LED   A2
 6
 7// variables for the red LED
 8bool ledState_RED = LOW;            // this holds the current state of the red LED
 9unsigned long prevMillis_RED = 0;   // this holds the start time of millis for computing the elapse time
10unsigned long interval_RED = 3000;  // this holds the interval of each toggle of LED
11
12// variables for the yellow LED
13bool ledState_YEL = LOW;            // this holds the current state of the LED
14unsigned long prevMillis_YEL = 0;   // this holds the start time of millis for computing the elapse time
15unsigned long interval_YEL = 5000;  // this holds the interval of each toggle of LED
16
17// variables for the blue led
18bool ledState_BLU = LOW;            // this holds the current state of the LED
19unsigned long prevMillis_BLU = 0;   // this holds the start time of millis for computing the elapse time
20unsigned long interval_BLU = 7000;  // this holds the interval of each toggle of LED
21
22unsigned long currMillis_ALL = 0;   // this holds the current time captured by the millis() function
23
24void setup() {
25  // Set the pin directions for LEDs
26  pinMode(RED_LED, OUTPUT);
27  pinMode(YEL_LED, OUTPUT);
28  pinMode(BLU_LED, OUTPUT);
29}
30
31void loop() {
32  // Store the currently time
33  // Notice: it use a single current time for synchonization with other function
34  currMillis_ALL = millis();
35
36  // Virtually process all the task
37  process_RED();
38  process_YEL();
39  process_BLU();
40}
41
42void process_RED() {
43  if (currMillis_ALL - prevMillis_RED >= interval_RED) {
44    // Elapse time has reached the interval
45    // Save the current time as previous time
46    prevMillis_RED = currMillis_ALL;
47
48    // toggle LED state
49    ledState_RED = !ledState_RED;
50    
51    digitalWrite(RED_LED, ledState_RED);
52  }
53}
54
55void process_YEL() {
56  if (currMillis_ALL - prevMillis_YEL >= interval_YEL) {
57    // Elapse time has reached the interval
58    // Save the current time as previous time
59    prevMillis_YEL = currMillis_ALL;
60
61    // toggle LED state
62    ledState_YEL = !ledState_YEL;
63    
64    digitalWrite(YEL_LED, ledState_YEL);
65  }
66}
67
68void process_BLU() {
69  if (currMillis_ALL - prevMillis_BLU >= interval_BLU) {
70    // Elapse time has reached the interval
71    // Save the current time as previous time
72    prevMillis_BLU = currMillis_ALL;
73
74    // toggle LED state
75    ledState_BLU = !ledState_BLU;
76    
77    digitalWrite(BLU_LED, ledState_BLU);
78  }
79}
 1
 2#include "LiquidCrystal.h"
 3
 4#define SW1 7
 5#define SW2 6
 6
 7LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
 8
 9void setup() {
10  // put your setup code here, to run once:
11  lcd.begin(16,2);
12
13  pinMode(SW1, INPUT_PULLUP);
14  pinMode(SW2, INPUT_PULLUP);
15
16  lcd.setCursor(0,0);
17  lcd.print("SW1 to delay    ");
18  lcd.setCursor(0,1);
19  lcd.print("You can press S2");
20}
21
22void loop() {
23  if (digitalRead(SW1) == LOW) {
24    lcd.setCursor(0,0);
25    lcd.println("SW1 is pressed  ");
26    lcd.setCursor(0,1);
27    lcd.println("Uno delay: 5 sec");
28    delay(5000);
29  }
30  if (digitalRead(SW2) == LOW) {
31    lcd.setCursor(0,1);
32    lcd.print("S2 is press     ");
33  } else {
34    lcd.setCursor(0,0);
35    lcd.print("SW1 to delay    ");
36    lcd.setCursor(0,1);
37    lcd.print("You can press S2");
38  }
39}
 1
 2// LED definitions for easy reference
 3#define RED_LED   A0
 4#define YEL_LED   A1
 5#define BLU_LED   A2
 6
 7bool ledState_RED = LOW;
 8unsigned long prevMillis_RED = 0;
 9unsigned long interval_RED_H = 800;
10unsigned long interval_RED_L = 200;
11unsigned long interval_RED = interval_RED_L;
12
13bool ledState_YEL = LOW;
14unsigned long prevMillis_YEL = 0;
15unsigned long interval_YEL_H = 2800;
16unsigned long interval_YEL_L = 200;
17unsigned long interval_YEL = interval_YEL_L;
18
19bool ledState_BLU = LOW;
20unsigned long prevMillis_BLU = 0;
21unsigned long interval_BLU_H = 8800; 
22unsigned long interval_BLU_L = 200; 
23unsigned long interval_BLU = interval_BLU_L;
24
25unsigned long currMillis_ALL = 0;
26
27
28void setup() {
29  // Set the pin directions for LEDs
30  pinMode(RED_LED, OUTPUT);
31  pinMode(YEL_LED, OUTPUT);
32  pinMode(BLU_LED, OUTPUT);
33}
34
35void loop() {
36  // Store the currently time
37  // Notice: it use a single current time for synchonization with other function
38  currMillis_ALL = millis();
39  process_RED();
40  process_YEL();
41  process_BLU();
42}
43
44void process_RED() {
45  if (currMillis_ALL - prevMillis_RED >= interval_RED) {
46    // Elapse time has reached the interval
47    // Save the current time as previous time
48    prevMillis_RED = currMillis_ALL;
49
50    // toggle LED state
51    if (ledState_RED == HIGH) {
52      ledState_RED = LOW;
53      interval_RED = interval_RED_L;
54    } else {
55      ledState_RED = HIGH;
56      interval_RED = interval_RED_H;
57    }
58    digitalWrite(RED_LED, ledState_RED);
59  }
60}
61
62void process_YEL() {
63  if (currMillis_ALL - prevMillis_YEL >= interval_YEL) {
64    // Elapse time has reached the interval
65    // Save the current time as previous time
66    prevMillis_YEL = currMillis_ALL;
67
68    // toggle LED state
69    if (ledState_YEL == HIGH) {
70      ledState_YEL = LOW;
71      interval_YEL = interval_YEL_L;
72    } else {
73      ledState_YEL = HIGH;
74      interval_YEL = interval_YEL_H;
75    }
76    
77    digitalWrite(YEL_LED, ledState_YEL);
78  }
79}
80
81void process_BLU() {
82  if (currMillis_ALL - prevMillis_BLU >= interval_BLU) {
83    // Elapse time has reached the interval
84    // Save the current time as previous time
85    prevMillis_BLU = currMillis_ALL;
86
87    // toggle LED state
88    if (ledState_BLU == HIGH) {
89      ledState_BLU = LOW;
90      interval_BLU = interval_BLU_L;
91    } else {
92      ledState_BLU = HIGH;
93      interval_BLU = interval_BLU_H;
94    }
95    digitalWrite(BLU_LED, ledState_BLU);
96  }
97}

Call To Action

I hope you find it useful and helpful. If you have any question, please do not hesitate to write it in the comment box.

Please Subscribe to TechToTinker Youtube channel:
Click me to Subscribe

Thank you.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.