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
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!