아두이노 나노 - 다중 버튼 | Arduino Nano - Multiple Button

이 튜토리얼은 Arduino Nano를 프로그래밍하는 방법을 안내하여 delay() 함수에 의존하지 않고 동시에 여러 버튼을 처리할 수 있게 합니다. 튜토리얼은 두 가지 다른 방법으로 코드를 제공합니다:

우리는 예시로 네 개의 버튼을 사용할 것입니다. 이를 쉽게 수정하여 두 개의 버튼, 세 개의 버튼, 다섯 개의 버튼 또는 그 이상으로 적용할 수 있습니다.

준비물

1×Arduino Nano Amazon
1×USB A to Mini-B USB cable 쿠팡 | Amazon
1×PCB-mount Button Amazon
1×Panel-mount Button Amazon
1×Breadboard 쿠팡 | Amazon
1×Jumper Wires Amazon
1×(Optional) 9V Power Adapter for Arduino Nano Amazon
1×(Recommended) Screw Terminal Adapter for Arduino Nano 쿠팡 | Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

버튼 정보

버튼(핀 배치, 작동 방식, 프로그래밍 방법 등)에 익숙하지 않다면, 다음 튜토리얼들이 더 많은 정보를 제공해 줄 수 있습니다:

선연결

Arduino Nano multiple button Wiring Diagram

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

아두이노 나노 코드 - 디바운스를 적용한 여러 버튼 처리

여러 버튼을 사용할 때 특정 상황에서는 복잡해질 수 있습니다:

  • 버튼 디바운싱을 요구하는 애플리케이션 (버튼에 디바운싱이 필요한 이유를 보려면 왜 버튼에 디바운싱이 필요한가를 참조)
  • 상태 변경(눌림/해제)을 감지해야 하는 애플리케이션

다행히도, ezButton library는 디바운스와 버튼 이벤트를 내부적으로 관리함으로써 이 과정을 간소화합니다. 이는 라이브러리를 사용할 때 타임스탬프와 변수를 관리하는 작업으로부터 사용자를 해방시켜줍니다. 추가적으로, 버튼 배열을 사용하면 코드의 명확성과 간결함을 향상시킬 수 있습니다.

/* * 이 Arduino Nano 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano/arduino-nano-multiple-button */ #include <ezButton.h> #define BUTTON_PIN_1 4 // 버튼 1에 연결된 아두이노 나노 핀 #define BUTTON_PIN_2 5 // 버튼 2에 연결된 아두이노 나노 핀 #define BUTTON_PIN_3 6 // 버튼 3에 연결된 아두이노 나노 핀 #define BUTTON_PIN_4 7 // 버튼 4에 연결된 아두이노 나노 핀 ezButton button1(BUTTON_PIN_1); // 버튼 1을 위한 ezButton 객체 생성 ezButton button2(BUTTON_PIN_2); // 버튼 2을 위한 ezButton 객체 생성 ezButton button3(BUTTON_PIN_3); // 버튼 3을 위한 ezButton 객체 생성 ezButton button4(BUTTON_PIN_4); // 버튼 4을 위한 ezButton 객체 생성 void setup() { Serial.begin(9600); button1.setDebounceTime(100); // 디바운스 시간을 100 밀리초로 설정 button2.setDebounceTime(100); // 디바운스 시간을 100 밀리초로 설정 button3.setDebounceTime(100); // 디바운스 시간을 100 밀리초로 설정 button4.setDebounceTime(100); // 디바운스 시간을 100 밀리초로 설정 } void loop() { button1.loop(); // 먼저 loop() 함수를 반드시 호출해야 합니다. button2.loop(); // 먼저 loop() 함수를 반드시 호출해야 합니다. button3.loop(); // 먼저 loop() 함수를 반드시 호출해야 합니다. button4.loop(); // 먼저 loop() 함수를 반드시 호출해야 합니다. // 디바운스 후의 버튼 상태 가져오기 int button1_state = button1.getState(); // 디바운스 후의 상태 int button2_state = button2.getState(); // 디바운스 후의 상태 int button3_state = button3.getState(); // 디바운스 후의 상태 int button4_state = button4.getState(); // 디바운스 후의 상태 /* 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"); }

사용 방법

위 이미지와 같이 배선하세요.

Arduino Nano 보드를 USB 케이블을 통해 PC에 연결하세요.

PC에서 Arduino IDE를 엽니다.

올바른 Arduino Nano 보드(예: Arduino Nano Uno)와 COM 포트를 선택하세요.

Arduino IDE의 왼쪽 바에서 Libraries 아이콘을 클릭하세요.

“ezButton”을 검색한 다음, Arduino NanoGetStarted에 의한 버튼 라이브러리를 찾으세요.

ezButton 라이브러리를 설치하려면 Install 버튼을 클릭하세요.

Arduino Nano button library
  • 위의 코드를 복사하여 아두이노 IDE에 붙여넣으세요.
  • 아두이노 IDE의 Upload 버튼을 클릭하여 코드를 아두이노 나노 보드에 컴파일하고 업로드하세요.
How to upload Arduino Nano code on Arduino IDE
  • 아두이노 IDE에서 시리얼 모니터 열기
  • 버튼을 하나씩 눌렀다 놓기
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
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

아두이노 나노 코드 - 배열을 사용한 다중 버튼

위의 코드를 버튼 배열을 사용하여 개선할 수 있습니다. 다음 코드는 이 배열을 사용하여 버튼 객체들을 처리합니다.

/* * 이 Arduino Nano 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano/arduino-nano-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 4 // 버튼의 수 #define BUTTON_PIN_1 4 // 버튼 1에 연결된 아두이노 나노 핀 #define BUTTON_PIN_2 5 // 버튼 2에 연결된 아두이노 나노 핀 #define BUTTON_PIN_3 6 // 버튼 3에 연결된 아두이노 나노 핀 #define BUTTON_PIN_4 7 // 버튼 4에 연결된 아두이노 나노 핀 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); // 디바운스 시간을 100밀리초로 설정 } } void loop() { for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // 먼저 loop() 함수를 호출해야 함 for (byte i = 0; i < BUTTON_NUM; i++) { // 디바운스 된 후의 버튼 상태 가져오기 int button_state = buttonArray[i].getState(); // 디바운스 된 후의 상태 /* 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"); } } }

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!