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:
ESP32 nhiều button với debounce
ESP32 nhiều button với debounce sử dụng mảng.
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.
Tiết lộ: Một số liên kết trong bài viết này là liên kết tiếp thị Amazon và có thể nhận hoa hồng theo chính sách của chương trình.
Về Button
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:
Các ứng dụng cần phát hiện thay đổi trạng thái (nhấn/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.
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-multiple-button */#include <ezButton.h>#define BUTTON_PIN_1 25 // The ESP32 pin GPIO25 connected to the button 1#define BUTTON_PIN_2 26 // The ESP32 pin GPIO26 connected to the button 2#define BUTTON_PIN_3 27 // The ESP32 pin GPIO27 connected to the button 3#define BUTTON_PIN_4 14 // The ESP32 pin GPIO14 connected to the button 4ezButtonbutton1(BUTTON_PIN_1); // create ezButton object for button 1ezButtonbutton2(BUTTON_PIN_2); // create ezButton object for button 2ezButtonbutton3(BUTTON_PIN_3); // create ezButton object for button 3ezButton button4(BUTTON_PIN_4); // create ezButton object for button 4voidsetup() {Serial.begin(9600);button1.setDebounceTime(100); // set debounce time to 100 millisecondsbutton2.setDebounceTime(100); // set debounce time to 100 millisecondsbutton3.setDebounceTime(100); // set debounce time to 100 milliseconds button4.setDebounceTime(100); // set debounce time to 100 milliseconds}voidloop() {button1.loop(); // MUST call the loop() function firstbutton2.loop(); // MUST call the loop() function firstbutton3.loop(); // MUST call the loop() function first button4.loop(); // MUST call the loop() function first// get button state after debounceint button1_state = button1.getState(); // the state after debounceint button2_state = button2.getState(); // the state after debounceint button3_state = button3.getState(); // the state after debounceint button4_state = button4.getState(); // the state after debounce/* Serial.print("The button 1 state: "); Serial.println(button1_state); Serial.print("The button 2 state: "); Serial.println(button2_state); Serial.print("The button 3 state: "); Serial.println(button3_state); Serial.print("The button 4 state: "); Serial.println(button4_state); */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");}
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.
Sao chép code trên và dán vào Arduino IDE.
Biên dịch và upload code lên bo mạch ESP32 bằng cách click nút Upload trên Arduino IDE
Mở Serial Monitor trên Arduino IDE
Nhấn và thả từng button một cách lần lượt
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
ESP32 Dev Module on COM15
2
Code ESP32 - Nhiều Button sử dụng mảng
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.
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-multiple-button */#include <ezButton.h>#define BUTTON_NUM 4 // the number of buttons#define BUTTON_PIN_1 25 // The ESP32 pin GPIO25 connected to the button 1#define BUTTON_PIN_2 26 // The ESP32 pin GPIO26 connected to the button 2#define BUTTON_PIN_3 27 // The ESP32 pin GPIO27 connected to the button 3#define BUTTON_PIN_4 14 // The ESP32 pin GPIO14 connected to the button 4ezButtonbuttonArray[] = {ezButton(BUTTON_PIN_1),ezButton(BUTTON_PIN_2),ezButton(BUTTON_PIN_3),ezButton(BUTTON_PIN_4)};voidsetup() {Serial.begin(9600);for (byte i = 0; i < BUTTON_NUM; i++) {buttonArray[i].setDebounceTime(100); // set debounce time to 100 milliseconds }}voidloop() {for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // MUST call the loop() function firstfor (byte i = 0; i < BUTTON_NUM; i++) {// get button state after debounceint button_state = buttonArray[i].getState(); // the state after debounce/* Serial.print("The button "); Serial.print(i + 1); Serial.print(": "); Serial.println(button_state); */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"); } }}
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.