Arduino Nút nhấn Khử rung

Khi một nút được nhấn/thả hoặc khi một công tắc được bật/tắt, người mới bắt đầu thường nghĩ đơn giản rằng trạng thái của nó được thay đổi từ LOW sang HIGH hoặc HIGH sang LOW. Trong thực tế, nó không phải lúc nào cũng đúng như vậy. Vì đặc tính cơ học và vật lý, trạng thái của nút (hoặc công tắc) có thể được thay đổi giữa LOW HIGH nhiều lần liên tiếp. Hiện tượng này được gọi là chattering. Hiện tượng chattering khiến một lần nhấn có thể được đọc như nhiều lần nhấn, dẫn đến sự cố ở một số loại ứng dụng. Hướng dẫn này cho thấy cách loại bỏ hiện tượng này (gọi là debounce đầu vào).

hiện tượng rung liên tục trên Arduino

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×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ề Nút

Nếu bạn chưa biết về nút nhấn (bố trí chân, cách nó 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 đây:

Sơ đồ đấu dây

sơ đồ đấu dây nút nhấn Arduino

This image is created using Fritzing. Click to enlarge image

Hãy xem và so sánh mã Arduino cho hai trường hợp: không có debounce và có debounce, cũng như hành vi của chúng.

Đọc nút nhấn mà không lọc nhiễu

Trước khi tìm hiểu về debouncing, hãy xem mã không có debouncing và hành vi của nó.

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

  • Kết nối Arduino với PC bằng cáp USB
  • Mở Arduino IDE, chọn board và cổng đúng
  • Sao chép đoạn mã dưới đây và mở bằng Arduino IDE
/* * 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-button-debounce */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if(lastState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last state lastState = currentState; }
  • Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên Arduino
nạp mã bằng Arduino ide
  • Mở Serial Monitor
  • Giữ nút nhấn trong vài giây rồi thả ra.
  • Xem kết quả trên Serial Monitor
COM6
Send
The button is pressed The button is pressed The button is pressed The button is released The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Như bạn có thể thấy, bạn đã nhấn và thả nút chỉ một lần duy nhất. Tuy nhiên, Arduino coi đó như nhiều lần nhấn và thả.

Đọc Nút với Chống Rung

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

  • Sao chép mã dưới đây và mở bằng Arduino IDE
/* * 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-button-debounce */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers // Variables will change: int lastSteadyState = LOW; // the previous steady state from the input pin int lastFlickerableState = LOW; // the previous flickerable state from the input pin int currentState; // the current reading from the input pin // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch/button changed, due to noise or pressing: if (currentState != lastFlickerableState) { // reset the debouncing timer lastDebounceTime = millis(); // save the the last flickerable state lastFlickerableState = currentState; } if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: // if the button state has changed: if (lastSteadyState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if (lastSteadyState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last steady state lastSteadyState = currentState; } }
  • Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Mở Serial Monitor
  • Nhấn và giữ nút trong vài giây rồi thả ra.
  • Xem kết quả trên Serial Monitor
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

⇒ Như bạn có thể thấy, bạn đã nhấn và thả nút đúng một lần. Arduino nhận diện nó là lần nhấn và thả duy nhất. Tiếng nhiễu được loại bỏ.

Chúng tôi đã làm cho nó đơn giản - Mã chống rung nút Arduino với thư viện

Để giúp người mới bắt đầu dễ dàng hơn nhiều, đặc biệt là khi sử dụng nhiều nút nhấn, chúng tôi đã tạo ra một thư viện có tên ezButton. Bạn có thể tìm hiểu về thư viện ezButton ở đây.

Mã chống rung cho nút Arduino cho một nút duy nhất

/* * 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> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) Serial.println("The button is pressed"); if(button.isReleased()) Serial.println("The button is released"); }

Mã khử rung nút Arduino cho nhiều 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-button-debounce */ #include <ezButton.h> ezButton button1(6); // create ezButton object that attach to pin 6; ezButton button2(7); // create ezButton object that attach to pin 7; ezButton button3(8); // create ezButton object that attach to pin 8; void setup() { Serial.begin(9600); button1.setDebounceTime(50); // set debounce time to 50 milliseconds button2.setDebounceTime(50); // set debounce time to 50 milliseconds button3.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button1.loop(); // MUST call the loop() function first button2.loop(); // MUST call the loop() function first button3.loop(); // MUST call the loop() function first if(button1.isPressed()) Serial.println("The button 1 is pressed"); if(button1.isReleased()) Serial.println("The button 1 is released"); if(button2.isPressed()) Serial.println("The button 2 is pressed"); if(button2.isReleased()) Serial.println("The button 2 is released"); if(button3.isPressed()) Serial.println("The button 3 is pressed"); if(button3.isReleased()) Serial.println("The button 3 is released"); }

Sơ đồ mạch cho đoạn mã ở trên:

sơ đồ nối dây cho thư viện nút Arduino

This image is created using Fritzing. Click to enlarge image

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.

Kiến thức bổ sung

  • DEBOUNCE_DELAY giá trị phụ thuộc vào các ứng dụng. Các ứng dụng khác nhau có thể sử dụng các giá trị khác nhau.

Khả năng mở rộng

Phương pháp debounce có thể áp dụng cho công tắc, cảm biến cảm ứng ...