Arduino LED Nháy LED không có độ trễ

Hãy tưởng tượng Arduino phải thực hiện hai nhiệm vụ: nháy đèn LED và kiểm tra trạng thái của nú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() (được mô tả trong Arduino - LED - Nhấp nháy), Arduino có thể bỏ lỡ một số sự kiện nhấn. Nói cách khác, Arduino không thể thực hiện đầy đủ nhiệm vụ thứ hai.

Trong bài hướng dẫn này, chúng ta sẽ học cách Arduino nháy LED và kiểm tra trạng thái của nút mà không bỏ lỡ bất kỳ sự kiện nhấn nút nào.

Chúng tôi sẽ xem qua ba ví dụ dưới đây và so sánh sự khác biệt giữa chúng.

※ Lưu ý:

  • Phương pháp này không chỉ dành cho việc nhấp nháy LED và kiểm tra trạng thái của nút. Nói chung, phương pháp này cho phép Arduino thực hiện đồng thời nhiều tác vụ mà không chặn lẫn nhau.
  • Tutorial này cung cấp kiến thức chuyên sâu giúp bạn hiểu nguyên lý hoạt động. Để làm cho mọi thứ dễ hiểu, bạn có thể sử dụng Arduino - Thư viện ezLED.

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

1×Arduino Uno R3
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (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 Block Shield for Arduino Uno
1×(Khuyến nghị) Breadboard Shield for Arduino Uno
1×(Khuyến nghị) Enclosure for Arduino Uno
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về LED và Nút nhấn

Nếu bạn chưa biết về LED và nút nhấn (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ đấu dây LED cho Arduino

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Với Độ trễ

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino/arduino-led-blink-without-delay */ // constants won't change: const int LED_PIN = 3; // the number of the LED pin const int BUTTON_PIN = 7; // the number of the button pin const long BLINK_INTERVAL = 1000; // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT); } void loop() { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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

  • Kết nối Arduino với máy tính bằng cáp USB
  • Mở Arduino IDE, chọn board và cổng đúng
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
Arduino ide - cách tải lên mã nguồn
  • Mở Serial Monitor
  • Nhấn nút 4 lần
  • Xem đèn LED: Đèn LED chuyển giữa BẬT và TẮT theo chu kỳ 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 đã bị bỏ sót. Điều đó xảy ra vì trong thời gian trễ, Arduino không thể làm bất cứ điều gì. Do đó, nó không thể phát hiện sự kiện nhấn.

Mã Arduino - Không Trì Hoãn

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino/arduino-led-blink-without-delay */ // constants won't change: const int LED_PIN = 3; // the number of the LED pin const int BUTTON_PIN = 7; // the number of the button pin const long BLINK_INTERVAL = 1000; // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis = 0; // will store last time LED was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: 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 currentMillis = millis(); if (currentMillis - previousMillis >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); // save the last time you blinked the LED previousMillis = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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

  • Chạy mã ở trên và nhấn nút 4 lần
  • Quan sát đèn LED: LED sẽ bật và tắt luân phiên theo chu kỳ mỗi giây
  • Xem đầu ra trong 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 sự kiện cấp bách đã được phát hiện.

Giải thích mã nguồn

Bạn có thể tìm lời giải thích trong các dòng chú thích của mã Arduino ở trên.

Thêm nhiều nhiệm vụ

Đoạn mã dưới đây nhấp nháy hai đèn LED với các chu kỳ khác nhau và kiểm tra trạng thái của nút.

/* * Mã Arduino này được phát triển bởi newbiely.vn * Mã Arduino 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/arduino/arduino-led-blink-without-delay */ // constants won't change: const int LED_PIN_1 = 3; // the number of the LED 1 pin const int LED_PIN_2 = LED_BUILTIN; // the number of the LED 2 pin const int BUTTON_PIN = 7; // the number of the button pin const long BLINK_INTERVAL_1 = 1000; // interval at which to blink LED 1 (milliseconds) const long BLINK_INTERVAL_2 = 500; // interval at which to blink LED 2 (milliseconds) // Variables will change: int ledState_1 = LOW; // ledState used to set the LED 1 int ledState_2 = LOW; // ledState used to set the LED 2 int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis_1 = 0; // will store last time LED 1 was updated unsigned long previousMillis_2 = 0; // will store last time LED 2 was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN_1, OUTPUT); pinMode(LED_PIN_2, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT); } void loop() { unsigned long currentMillis = millis(); // check to see if it's time to blink the LED 1 if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: ledState_1 = (ledState_1 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_1, ledState_1); // save the last time you blinked the LED previousMillis_1 = currentMillis; } // check to see if it's time to blink the LED 2 if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: ledState_2 = (ledState_2 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_2, ledState_2); // save the last time you blinked the LED previousMillis_2 = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if(currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // 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 có thể được dùng để cho Arduino thực hiện nhiều tác vụ cùng lúc mà không chặn lẫn nhau. Ví dụ, gửi một yêu cầu đến Internet và chờ phản hồi; trong khi chờ phản hồi, nhấp nháy một số đèn LED và kiểm tra nút hủy.