ESP32 Nhiều Button
Hướng dẫn này sẽ chỉ bạn cách lập trình ESP32 để sử dụng nhiều button cùng lúc mà không cần phụ thuộc vào hàm delay(). Hướng dẫn cung cấp mã nguồn theo hai phương pháp:
Chúng ta sẽ minh họa với bốn button. Tuy nhiên, bạn có thể dễ dàng điều chỉnh mã cho hai button, ba button, năm button, hoặc thậm chí nhiều hơn.
| 1 | × | mô-đun phát triển ESP-WROOM-32 | | |
| 1 | × | Alternatively, ESP32 Uno-form board | | |
| 1 | × | Alternatively, ESP32 S3 Uno-form board | | |
| 1 | × | Cáp USB Type-C | | |
| 1 | × | breadboard-mount Button with Cap | | |
| 1 | × | breadboard-mount Button Kit | | |
| 1 | × | Panel-mount Button | | |
| 1 | × | mô-đun nút nhấn | | |
| 1 | × | breadboard | | |
| 1 | × | Dây Jumper | | |
| 1 | × | (Khuyến nghị) Screw Terminal Expansion Board for ESP32 | | |
| 1 | × | (Khuyến nghị) Breakout Expansion Board for ESP32 | | |
| 1 | × | (Khuyến nghị) Power Splitter for ESP32 | | |
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | | |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) | | |
Chúng tôi có các hướng dẫn chi tiết về button. Mỗi hướng dẫn chứa thông tin chi tiết và hướng dẫn từng bước về pinout phần cứng, nguyên lý hoạt động, kết nối mạch với ESP32, code ESP32... Tìm hiểu thêm tại các liên kết sau:

This image is created using Fritzing. Click to enlarge image
Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.
Khi làm việc với nhiều button, độ phức tạp có thể phát sinh trong những tình huống cụ thể:
May mắn thay, thư viện ezButton đơn giản hóa quá trình này bằng cách xử lý debounce và các sự kiện button bên trong. Điều này giúp người dùng không cần quản lý timestamps và biến khi sử dụng thư viện. Hơn nữa, việc sử dụng mảng button có thể cải thiện độ rõ ràng và ngắn gọn của code.
#include <ezButton.h>
#define BUTTON_PIN_1 25
#define BUTTON_PIN_2 26
#define BUTTON_PIN_3 27
#define BUTTON_PIN_4 14
ezButton button1(BUTTON_PIN_1);
ezButton button2(BUTTON_PIN_2);
ezButton button3(BUTTON_PIN_3);
ezButton button4(BUTTON_PIN_4);
void setup() {
Serial.begin(9600);
button1.setDebounceTime(100);
button2.setDebounceTime(100);
button3.setDebounceTime(100);
button4.setDebounceTime(100);
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
button4.loop();
int button1_state = button1.getState();
int button2_state = button2.getState();
int button3_state = button3.getState();
int button4_state = button4.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");
}
Thực hiện đấu nối như hình trên.
Kết nối bo mạch ESP32 với PC của bạn qua cáp micro USB
Mở Arduino IDE trên PC.
Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
Click vào biểu tượng Libraries ở thanh bên trái của Arduino IDE.
Tìm kiếm "ezButton", sau đó tìm thư viện button của ArduinoGetStarted
Click 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
Chúng ta có thể cải thiện code đã cung cấp bằng cách sử dụng mảng button. Code sau đây minh họa cách mảng này quản lý các đối tượng button.
#include <ezButton.h>
#define BUTTON_NUM 4
#define BUTTON_PIN_1 25
#define BUTTON_PIN_2 26
#define BUTTON_PIN_3 27
#define BUTTON_PIN_4 14
ezButton buttonArray[] = {
ezButton(BUTTON_PIN_1),
ezButton(BUTTON_PIN_2),
ezButton(BUTTON_PIN_3),
ezButton(BUTTON_PIN_4)
};
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.