Khi lập trình Arduino Uno R4 để phát hiện sự kiện nhấn nút (button press), bạn có thể thấy rằng một lần nhấn được phát hiện nhiều lần. Điều này xảy ra vì do các yếu tố cơ học, button hoặc switch có thể chuyển đổi nhanh chóng giữa LOW và HIGH nhiều lần. Hiện tượng này được gọi là "chattering" (rung phím). Chattering có thể khiến một lần nhấn nút được phát hiện như nhiều lần nhấn, điều này có thể gây ra lỗi trong một số ứng dụng. Tutorial này giải thích cách khắc phục vấn đề này, một quá trình được gọi là debouncing button.
Phần Cứng Cần Thiết
1
×
Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1
×
(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
This image is created using Fritzing. Click to enlarge image
Hãy cùng kiểm tra và so sánh code Arduino UNO R4 không có và có debounce, và quan sát hoạt động của chúng.
Arduino Uno R4 - Button không có Debounce
Trước khi chúng ta tìm hiểu về debouncing, hãy xem code không có debounce và quan sát cách hoạt động của nó.
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-button-debounce */#define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the buttonint button_state; // the current state of buttonint prev_button_state = LOW; // the previous state of buttonvoidsetup() {// initialize serial communication at 9600 bits per second:Serial.begin(9600);// initialize the pushbutton pin as a pull-up inputpinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the state of the switch/button: button_state = digitalRead(BUTTON_PIN);if (prev_button_state == HIGH && button_state == LOW)Serial.println("The button is pressed");elseif (prev_button_state == LOW && button_state == HIGH)Serial.println("The button is released");// save the last state prev_button_state = button_state;}
Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
Khởi động Arduino IDE trên máy tính của bạn.
Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và COM port.
Sao chép code trên và mở trong Arduino IDE.
Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4.
Mở Serial Monitor.
Nhấn và giữ button trong vài giây, sau đó thả ra.
Kiểm tra kết quả trong Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
The button is pressed
The button is pressed
The button is pressed
The button is released
The button is released
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Như bạn có thể thấy, bạn chỉ nhấn và thả button một lần. Tuy nhiên, Arduino nhận biết nó như nhiều lần nhấn và thả.
※ Lưu ý:
Giá trị của DEBOUNCE_TIME thay đổi tùy theo từng ứng dụng khác nhau. Mỗi ứng dụng có thể sử dụng một giá trị riêng.
Arduino Uno R4 - Button có Debounce
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-button-debounce */#define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button#define DEBOUNCE_TIME 50 // The debounce time; increase if the output flickersint last_steady_state = LOW; // the previous steady state from the input pinint last_flickerable_state = LOW; // the previous flickerable state from the input pinint current_state; // the current reading from the input pinunsignedlong last_debounce_time = 0; // the last time the output pin was toggledvoidsetup() {// initialize serial communication at 9600 bits per second:Serial.begin(9600);// initialize the pushbutton pin as a pull-up inputpinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the state of the switch/button: current_state = digitalRead(BUTTON_PIN);// If the switch/button changed, due to noise or pressing:if (current_state != last_flickerable_state) {// reset the debouncing timer last_debounce_time = millis();// save the the last flickerable state last_flickerable_state = current_state; }if ((millis() - last_debounce_time) > DEBOUNCE_TIME) {// if the button state has changed:if (last_steady_state == HIGH && current_state == LOW)Serial.println("The button is pressed");elseif (last_steady_state == LOW && current_state == HIGH)Serial.println("The button is released");// save the the last steady state last_steady_state = current_state; }}
Các Bước Thực Hiện
Sao chép code trên và mở với Arduino IDE.
Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4.
Mở Serial Monitor.
Nhấn giữ button trong vài giây trước khi thả ra.
Kiểm tra kết quả trong Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
The button is pressed
The button is released
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Như bạn có thể thấy, bạn nhấn và thả button một lần. Arduino phát hiện chính xác như một lần nhấn và thả, loại bỏ mọi hiện tượng chattering.
Chúng Tôi Đã Làm Cho Nó Đơn Giản: Code Arduino UNO R4 Button Debounce Sử Dụng Thư Viện
Chúng tôi đã tạo ra một cách đơn giản hơn cho người mới bắt đầu sử dụng nhiều button bằng cách tạo một thư viện tên là ezButton. Bạn có thể tìm hiểu thêm về thư viện ezButton tại đây.
Hãy xem một số code ví dụ.
Code Arduino UNO R4 Button Debounce Cho Một Button Đơn
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library * * This example reads the state of a button with debounce and print it to Serial Monitor. */#include <ezButton.h>ezButtonbutton(7); // create ezButton object that attach to pin 7;voidsetup() {Serial.begin(9600);button.setDebounceTime(50); // set debounce time to 50 milliseconds}voidloop() {button.loop(); // MUST call the loop() function firstif(button.isPressed())Serial.println("The button is pressed");if(button.isReleased())Serial.println("The button is released");}
Code Arduino UNO R4 Button Debounce Cho Nhiều Button
Hãy debounce cho 3 button. Đây là sơ đồ đấu nối giữa Arduino UNO R4 và ba button:
This image is created using Fritzing. Click to enlarge image
/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-button-debounce */#include <ezButton.h>ezButton button_1(6); // initialize ezButton object connected to pin 6ezButton button_2(7); // initialize ezButton object connected to pin 7ezButton button_3(8); // initialize ezButton object connected to pin 8voidsetup() {Serial.begin(9600); button_1.setDebounceTime(50); // configure debounce time for button_1 to 50ms button_2.setDebounceTime(50); // configure debounce time for button_2 to 50ms button_3.setDebounceTime(50); // configure debounce time for button_3 to 50ms}voidloop() { button_1.loop(); // update button_1 state button_2.loop(); // update button_2 state button_3.loop(); // update button_3 stateif(button_1.isPressed())Serial.println("The button 1 is pressed");if(button_1.isReleased())Serial.println("The button 1 is released");if(button_2.isPressed())Serial.println("The button 2 is pressed");if(button_2.isReleased())Serial.println("The button 2 is released");if(button_3.isPressed())Serial.println("The button 3 is pressed");if(button_3.isReleased())Serial.println("The button 3 is released");}
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.