ESP8266 đèn LED Nháy mà không dùng delay()

Giả sử ESP8266 có hai nhiệm vụ cần thực hiện: nháy một đèn LED và giám sát trạng thái của một nút có thể được nhấn bất cứ lúc nào. Nếu chúng ta sử dụng hàm [delay()] (như được giải thích trong ESP8266 - Đèn LED), ESP8266 có thể bỏ lỡ một số lần nhấn nút. Nói cách khác, ESP8266 sẽ không thể thực hiện đầy đủ nhiệm vụ thứ hai.

Bài hướng dẫn này chỉ cho bạn cách làm cho ESP8266 nhấp nháy một đèn LED và theo dõi trạng thái của một nút bấm mà không bỏ lỡ bất kỳ lần nhấn nào.

Chúng ta sẽ xem qua ba ví dụ và so sánh những khác biệt giữa chúng:

Phương pháp này không chỉ giới hạn ở việc nhấp nháy đèn LED và kiểm tra trạng thái của nút. Nó cho phép ESP8266 thực hiện nhiều tác vụ đồng thời mà không làm gián đoạn lẫn nhau.

Phần cứng cần chuẩn bị

1×ESP8266 NodeMCU ESP-12E
1×Recommended: ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×breadboard-mount Button with Cap
1×breadboard-mount Button Kit
1×Panel-mount Push Button
1×mô-đun nút nhấn
1×breadboard
1×dây jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP8266
1×(Khuyến nghị) Power Splitter for ESP8266 Type-C

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về LED và Nút

Nếu bạn chưa quen với LED và nút nhấn (bố trí chân, chức năng, lập trình ...), các bài hướng dẫn sau đây có thể giúp bạn:

Sơ đồ đấu dây

sơ đồ nối dây LED ESP8266 NodeMCU

This image is created using Fritzing. Click to enlarge image

Xem thêm Sơ đồ chân ESP8266Cách cấp nguồn cho ESP8266.

Mã ESP8266 - Với độ trễ

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-led-blink-without-delay */ #define BUTTON_PIN D1 // The ESP8266 pin D1 connected to button #define LED_PIN D7 // The ESP8266 pin D7 connected to led #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) int led_state = LOW; // led_state used to set the LED int prev_button_state = LOW; // will store last time button was updated void setup() { Serial.begin(9600); // Configure the ESP8266 pin as a digital output pin pinMode(LED_PIN, OUTPUT); // Configure the ESP8266 pin as a digital input pin pinMode(BUTTON_PIN, INPUT); } void loop() { // if the LED is off turn it on and vice-versa: led_state == (led_state == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN, led_state); delay(BLINK_INTERVAL); // If button is pressed during this time, ESP8266 CANNOT detect int button_state = digitalRead(BUTTON_PIN); if(button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

Hướng dẫn từng bước

Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:

  • Xem hướng dẫn cách thiết lập môi trường cho ESP8266 trên Arduino IDE (BASE_URL/tutorials/esp8266/esp8266-software-installation) nếu đây là lần đầu bạn sử dụng ESP8266.
  • Gắn kết các linh kiện như trong sơ đồ.
  • Kết nối bo mạch ESP8266 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 phù hợp, ví dụ NodeMCU 1.0 (Mô-đun ESP-12E), và cổng COM tương ứng của nó.
  • Cắm cáp USB vào ESP8266 và máy tính.
  • Khởi động Arduino IDE, chọn bo mạch và cổng đúng.
  • Sao chép mã và mở nó trong Arduino IDE.
  • Nhấn nút Tải lên trong Arduino IDE để biên dịch và tải mã lên ESP8266.
cách nạp mã nguồn vào ESP8266 NodeMCU bằng Arduino ide
  • Mở Serial Monitor.
  • Nhấn nút bốn lần.
  • Kiểm tra LED; nó sẽ luân phiên bật và tắt cứ mỗi giây.
  • Xem đầu ra trong Serial Monitor.
COM6
Send
1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Trên Serial Monitor, một số lần nhấn nút đã không được ghi nhận. Điều này là do trong khi đang ở trạng thái delay, ESP8266 không thể thực hiện bất kỳ tác vụ nào. Do đó, nó không thể phát hiện sự kiện nhấn nút.

Mã ESP8266 - Không có độ trễ

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-led-blink-without-delay */ #define BUTTON_PIN D1 // The ESP8266 pin D1 connected to button #define LED_PIN D7 // The ESP8266 pin D7 connected to led #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) int led_state = LOW; // led_state used to set the LED int prev_button_state = LOW; // will store last time button was updated unsigned long prev_time_ms = 0; // will store last time LED was updated void setup() { Serial.begin(9600); // Configure the ESP8266 pin as a digital output pin pinMode(LED_PIN, OUTPUT); // Configure the ESP8266 pin as a digital input pin pinMode(BUTTON_PIN, INPUT); } void loop() { // 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 time_ms = millis(); if (time_ms - prev_time_ms >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: led_state == (led_state == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN, led_state); // save the last time you blinked the LED prev_time_ms = time_ms; } // check button state's change int button_state = digitalRead(BUTTON_PIN); if(button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

Hướng dẫn từng bước

  • Nối các thành phần như hình vẽ.
  • Kết nối bo mạch ESP8266 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 đúng, ví dụ như NodeMCU 1.0 (ESP-12E Module), và cổng COM tương ứng.
  • Chạy mã và nhấn nút 4 lần.
  • Đèn LED sẽ luân phiên giữa BẬT và TẮT với chu kỳ một giây.
  • Kiểm tra đầu ra trên Serial Monitor.
COM6
Send
1 0 1 0 1 0 1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tất cả các trường hợp khẩn cấp đã được xác định.

Giải thích mã nguồn

Hãy xem giải thích theo từng dòng được chứa trong các bình luận của mã nguồn!

Thêm nhiều nhiệm vụ

Mã ESP8266 ở dưới đây làm

  • Làm cho hai đèn LED nhấp nháy với các chu kỳ khác nhau.
  • Kiểm tra trạng thái của nút nhấn.
/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp8266/esp8266-led-blink-without-delay */ #define BUTTON_PIN D1 // The ESP8266 pin D1 connected to button #define LED_PIN_1 D6 // The ESP8266 pin D7 connected to led #define LED_PIN_2 D7 // The ESP8266 pin D7 connected to led #define BLINK_INTERVAL_1 1000 // interval at which to blink LED 1 (milliseconds) #define BLINK_INTERVAL_2 500 // interval at which to blink LED 2 (milliseconds) int led_state_1 = LOW; // led_state used to set the LED 1 int led_state_2 = LOW; // led_state used to set the LED 2 int prev_button_state = LOW; // will store last time button was updated unsigned long prev_time_ms_1 = 0; // will store last time LED 1 was updated unsigned long prev_time_ms_2 = 0; // will store last time LED 2 was updated void setup() { Serial.begin(9600); // Configure the ESP8266 pin as a digital output pin pinMode(LED_PIN_1, OUTPUT); pinMode(LED_PIN_2, OUTPUT); // Configure the ESP8266 pin as a digital input pin pinMode(BUTTON_PIN, INPUT); } void loop() { unsigned long time_ms = millis(); // check to see if it's time to blink the LED 1 if (time_ms - prev_time_ms_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: led_state_1 == (led_state_1 == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN_1, led_state_1); // save the last time you blinked the LED prev_time_ms_1 = time_ms; } // check to see if it's time to blink the LED 2 if (time_ms - prev_time_ms_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: led_state_2 == (led_state_2 == LOW) ? HIGH : LOW; // set the LED with the led_state of the variable: digitalWrite(LED_PIN_2, led_state_2); // save the last time you blinked the LED prev_time_ms_1 = time_ms; } // check button state's change int button_state = digitalRead(BUTTON_PIN); if(button_state != prev_button_state) { // print out the state of the button: Serial.println(button_state); // save the last state of button prev_button_state = button_state; } // DO OTHER WORKS HERE }

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Khả năng mở rộng

Phương pháp này cho phép ESP8266 thực hiện đồng thời nhiều tác vụ mà không tác vụ nào chặn tác vụ khác. Ví dụ, gửi một yêu cầu tới Internet và đợi phản hồi, đồng thời nháy các đèn LED và theo dõi nút hủy.

Các tham chiếu hàm