Nhiều nút nhấn Arduino

Hướng dẫn này dạy bạn cách lập trình Arduino để đồng thời sử dụng nhiều nút mà không dùng hàm delay(). Hướng dẫn cung cấp mã theo hai cách:

Chúng tôi sẽ sử dụng năm nút làm ví dụ. Bạn có thể dễ dàng chỉnh sửa nó để phù hợp với hai nút, bốn nút, hoặc thậm chí nhiều nút hơn.

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 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

Chúng tôi có một hướng dẫn chi tiết về nút nhấn, bao gồm sơ đồ chân của phần cứng, nguyên lý hoạt động, cách kết nối Arduino và các hướng dẫn mã nguồn. Tìm hiểu thêm tại đây:

Sơ đồ đấu dây

sơ đồ nối dây Arduino cho nhiều nút nhấn

This image is created using Fritzing. Click to enlarge image

Mã Arduino - Nhiều nút có debounce

Khi sử dụng nhiều nút, mọi thứ có thể trở nên phức tạp ở một số tình huống:

May mắn thay, thư viện ezButton library này giúp quá trình này trở nên đơn giản bằng cách xử lý chống rung và các sự kiện nút ở bên trong. Điều này giải phóng người dùng khỏi nhiệm vụ quản lý dấu thời gian và các biến khi sử dụng thư viện. Thêm vào đó, việc sử dụng một mảng các nút có thể làm tăng tính rõ ràng và ngắn gọn của mã.

/* * 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-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // the number of buttons #define BUTTON_PIN_1 2 // The Arduino pin connected to the button 1 #define BUTTON_PIN_2 3 // The Arduino pin connected to the button 2 #define BUTTON_PIN_3 4 // The Arduino pin connected to the button 3 #define BUTTON_PIN_4 5 // The Arduino pin connected to the button 4 #define BUTTON_PIN_5 6 // The Arduino pin connected to the button 5 ezButton button1(BUTTON_PIN_1); // create ezButton object for button 1 ezButton button2(BUTTON_PIN_2); // create ezButton object for button 2 ezButton button3(BUTTON_PIN_3); // create ezButton object for button 3 ezButton button4(BUTTON_PIN_4); // create ezButton object for button 4 ezButton button5(BUTTON_PIN_5); // create ezButton object for button 5 void setup() { Serial.begin(9600); button1.setDebounceTime(100); // set debounce time to 100 milliseconds button2.setDebounceTime(100); // set debounce time to 100 milliseconds button3.setDebounceTime(100); // set debounce time to 100 milliseconds button4.setDebounceTime(100); // set debounce time to 100 milliseconds button5.setDebounceTime(100); // set debounce time to 100 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 button4.loop(); // MUST call the loop() function first button5.loop(); // MUST call the loop() function first // get button state after debounce int button1_state = button1.getState(); // the state after debounce int button2_state = button2.getState(); // the state after debounce int button3_state = button3.getState(); // the state after debounce int button4_state = button4.getState(); // the state after debounce int button5_state = button5.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); Serial.print("The button 5 state: "); Serial.println(button5_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"); if (button5.isPressed()) Serial.println("The button 5 is pressed"); if (button5.isReleased()) Serial.println("The button 5 is released"); }

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

  • Lắp đặt dây điện như hình ở trên.
  • Kết nối bo mạch Arduino với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino phù hợp (ví dụ Arduino Uno) và cổng COM.
  • Nhấn 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 nút do ArduinoGetStarted cung cấp.
  • Nhấp vào nút Install để cài đặt thư viện ezButton.
thư viện nút nhấn Arduino
  • Sao chép mã ở trên và dán vào Arduino IDE.
  • Biên dịch và tải mã lên bo mạch Arduino bằng cách nhấp vào nút Tải lên trên Arduino IDE
cách tải mã Arduino lên Arduino ide
  • Mở Serial Monitor trên Arduino IDE
  • Nhấn và thả nút lần lượt
COM6
Send
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
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Mã Arduino - Nhiều nút sử dụng mảng

Chúng ta có thể cải thiện mã ở trên bằng cách sử dụng một mảng các nút. Đoạn mã sau đây sử dụng mảng này để xử lý các đối tượng 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-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // the number of buttons #define BUTTON_PIN_1 2 // The Arduino pin connected to the button 1 #define BUTTON_PIN_2 3 // The Arduino pin connected to the button 2 #define BUTTON_PIN_3 4 // The Arduino pin connected to the button 3 #define BUTTON_PIN_4 5 // The Arduino pin connected to the button 4 #define BUTTON_PIN_5 6 // The Arduino pin connected to the button 5 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); // set debounce time to 100 milliseconds } } void loop() { for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // MUST call the loop() function first for (byte i = 0; i < BUTTON_NUM; i++) { // get button state after debounce int 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.