Arduino UNO R4 nhiều Button
Hướng dẫn này sẽ chỉ cho bạn cách sử dụng Arduino UNO R4 với nhiều button cùng lúc mà không cần hàm delay() để debounce. Hướng dẫn cung cấp code theo hai phương pháp khác nhau:
Chúng ta sẽ sử dụng năm button làm ví dụ. Bạn có thể dễ dàng thay đổi cho hai button, bốn button, hoặc thậm chí nhiều hơn.
| 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 | | |
| 1 | × | Arduino UNO R4 Minima (Thay thế) | | |
| 1 | × | Cáp USB Type-C | | |
| 1 | × | Nút Nhấn Gắn Breadboard có Nắp | | |
| 1 | × | Bộ Kit Nút Nhấn Gắn Breadboard | | |
| 1 | × | Nút Nhấn Gắn Panel | | |
| 1 | × | mô-đun nút nhấn | | |
| 1 | × | breadboard | | |
| 1 | × | Dây Jumper | | |
| 1 | × | (Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Breadboard Shield for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Enclosure for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Power Splitter for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO | | |
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | | |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) | | |
Chúng tôi cung cấp hướng dẫn chi tiết về button, bao gồm kết nối phần cứng, cách hoạt động, đấu dây với Arduino UNO R4, và cách viết code. Tìm hiểu thêm tại đây:

This image is created using Fritzing. Click to enlarge image
Khi sử dụng nhiều button, tình huống có thể trở nên phức tạp.
Thư viện ezButton giúp đơn giản hóa việc làm việc với button bằng cách xử lý debounce và các sự kiện button bên trong. Người dùng không cần lo lắng về việc quản lý thời gian và biến khi sử dụng thư viện này. Ngoài ra, việc sử dụng nhiều button có thể làm cho code rõ ràng và ngắn gọn hơn.
#include <ezButton.h>
#define BUTTON_NUM 5
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define BUTTON_PIN_3 4
#define BUTTON_PIN_4 5
#define BUTTON_PIN_5 6
ezButton button1(BUTTON_PIN_1);
ezButton button2(BUTTON_PIN_2);
ezButton button3(BUTTON_PIN_3);
ezButton button4(BUTTON_PIN_4);
ezButton button5(BUTTON_PIN_5);
void setup() {
Serial.begin(9600);
button1.setDebounceTime(100);
button2.setDebounceTime(100);
button3.setDebounceTime(100);
button4.setDebounceTime(100);
button5.setDebounceTime(100);
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();
int button1_state = button1.getState();
int button2_state = button2.getState();
int button3_state = button3.getState();
int button4_state = button4.getState();
int button5_state = button5.getState();
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");
if (button4.isPressed())
Serial.println("The button 4 is pressed");
if (button4.isReleased())
Serial.println("The button 4 is released");
if (button5.isPressed())
Serial.println("The button 5 is pressed");
if (button5.isReleased())
Serial.println("The button 5 is released");
}
Thực hiện theo hướng dẫn từng bước:
Kết nối Arduino Uno R4 với các button theo sơ đồ được cung cấp.
Kết nối bo mạch 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 bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
Nhấp vào biểu tượng Libraries ở phía bên trái của Arduino IDE.
Tìm kiếm ezButton và tìm thư viện button được tạo bởi ArduinoGetStarted.com.
Nhấp vào nút Install để cài đặt thư viện ezButton.
The button 1 is pressed
The button 1 is released
The button 2 is pressed
The button 2 is released
The button 3 is pressed
The button 3 is released
The button 4 is pressed
The button 4 is released
The button 5 is pressed
The button 5 is released
Chúng ta có thể cải thiện code trên bằng cách sử dụng mảng button.
#include <ezButton.h>
#define BUTTON_NUM 5
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define BUTTON_PIN_3 4
#define BUTTON_PIN_4 5
#define BUTTON_PIN_5 6
ezButton buttonArray[] = {
ezButton(BUTTON_PIN_1),
ezButton(BUTTON_PIN_2),
ezButton(BUTTON_PIN_3),
ezButton(BUTTON_PIN_4),
ezButton(BUTTON_PIN_5)
};
void setup() {
Serial.begin(9600);
for (byte i = 0; i < BUTTON_NUM; i++) {
buttonArray[i].setDebounceTime(100);
}
}
void loop() {
for (byte i = 0; i < BUTTON_NUM; i++)
buttonArray[i].loop();
for (byte i = 0; i < BUTTON_NUM; i++) {
int button_state = buttonArray[i].getState();
if (buttonArray[i].isPressed()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is pressed");
}
if (buttonArray[i].isReleased()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is released");
}
}
}
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.