Arduino Uno R4의 특징 중 하나로 USB 주변기기에 의한 HID 키보드 장치가 있습니다. 이번 시간에는 Arduino Uno R4의 HID 키보드 장치를 만들어 보도록 하겠습니다.
USB HID 장치란?
컴퓨터의 키보드나 마우스로서 문자 입력이나 마우스 커서 조작을 마이크로 컴퓨터에서 할 수 있는 기능입니다. 키보드와 마우스 입력 기능을 가진 아두이노 정품으로는 ATmega32u4를 탑재한 아두이노 레오나르도, 아두이노 마이크로가 있습니다. Arduino Leonardo는 4000엔 정도로 비싼 반면, Arduino Uno R4 Minima는 USB를 지원하며 가격도 Arduino Leonardo보다 저렴합니다. 또한, 교체용 보드는 2000엔 정도로 Arduino Uno의 호환 보드와 비교하면 비싸긴 하지만 저렴하다고 할 수 있습니다. 다만, Uno 모양이 아니어도 괜찮다면 Ch552나 RaspberryPiPico 등이 더 저렴하다고 할 수 있습니다.
샘플 프로그램으로 Arduino의 샘플 프로그램 Keyboard and Mouse Control을 참고합니다.
필수 코드 사용하려면 아래 코드가 필요합니다.
#include "Keyboard.h"
Keyboard.begin();
Keyboard.write('x');
Button 프로그램에 포팅한 예시
아래 코드를 Button 프로그램에 포팅합니다.
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState_old = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState != buttonState_old) {
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Keyboard.write('u');
}
}
buttonState_old = buttonState;
delay(100);
}
buttonPin인 2 핀을 GND에 연결하면 LED와 함께 u 라는 글자가 입력된다.
여러 종류의 입력에 대응하는, 또한 여러 개의 입력 핀을 지원하는 프로그램으로 만듭니다.
/*
Button to HID Custom!
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"
// constants won't change. They're used here to set pin numbers:
const int Button2 = 2; // the number of the pushbutton pin
const int Button3 = 3; // the number of the pushbutton pin
const int Button4 = 4; // the number of the pushbutton pin
const int Button5 = 5; // the number of the pushbutton pin
const int Button6 = 6; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status
int buttonState5 = 0; // variable for reading the pushbutton status
int buttonState6 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// read the state of the pushbutton value:
buttonState2 = digitalRead(Button2);
// check if the pushbutton is pressed. If it is, the buttonState is LOW:
if (buttonState2 == LOW) {
// Keyboard Write:
Keyboard.write('w');
}
buttonState3 = digitalRead(Button3);
if (buttonState3 == LOW) {
Keyboard.write('e');
}
buttonState4 = digitalRead(Button4);
if (buttonState4 == LOW) {
Keyboard.write('r');
}
buttonState5 = digitalRead(Button5);
if (buttonState5 == LOW) {
Keyboard.write('t');
}
buttonState6 = digitalRead(Button6);
if (buttonState6 == LOW) {
Keyboard.write('y');
}
delay(100);
}
이전 입력 문자와 다를 때만 입력하는 프로그램으로 만들기
이전 프로그램의 경우, 버튼을 계속 누르고 있으면, delay(100); 마다 마다 문자 입력이 계속됩니다. 한 번 감지하면 두 번째는 보내지 않는 프로그램으로 변경합니다.
/*
Button to HID Custom!
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"
// constants won't change. They're used here to set pin numbers:
const int Button2 = 2; // the number of the pushbutton pin
const int Button3 = 3; // the number of the pushbutton pin
const int Button4 = 4; // the number of the pushbutton pin
const int Button5 = 5; // the number of the pushbutton pin
const int Button6 = 6; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState2_old = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState3_old = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status
int buttonState4_old = 0; // variable for reading the pushbutton status
int buttonState5 = 0; // variable for reading the pushbutton status
int buttonState5_old = 0; // variable for reading the pushbutton status
int buttonState6 = 0; // variable for reading the pushbutton status
int buttonState6_old = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// read the state of the pushbutton value:
buttonState2 = digitalRead(Button2);
if (buttonState2 != buttonState2_old) {
// check if the pushbutton is pressed. If it is, the buttonState is LOW:
if (buttonState2 == LOW) {
// Keyboard Write:
Keyboard.write('w');
}
}
buttonState3 = digitalRead(Button3);
if (buttonState3 != buttonState3_old) {
if (buttonState3 == LOW) {
Keyboard.write('eu');
}
}
buttonState4 = digitalRead(Button4);
if (buttonState4 != buttonState4_old) {
if (buttonState4 == LOW) {
Keyboard.write('r');
}
}
buttonState5 = digitalRead(Button5);
if (buttonState5 != buttonState5_old) {
if (buttonState5 == LOW) {
Keyboard.write('t');
}
}
buttonState6 = digitalRead(Button6);
if (buttonState6 != buttonState6_old) {
if (buttonState6 == LOW) {
Keyboard.write('y');
}
}
buttonState2_old = buttonState2;
buttonState3_old = buttonState3;
buttonState4_old = buttonState4;
buttonState5_old = buttonState5;
buttonState6_old = buttonState6;
delay(100);
}
이상으로 버튼을 계속 눌러도 한 번만 문자를 보내는 프로그램이 완성된 것 같습니다. 코드 자체는 매우 간단하고, 힘도 많이 들지 않습니다. buttonState가 buttonState_old와 다른 경우 그리고 buttonState가 LOW인 경우에만 문자 입력을 하는 프로그램이 됩니다.

'아두이노우노 R4' 카테고리의 다른 글
| 아두이노 우노 R4 설치 - Arduino IDE 2.0 (0) | 2024.05.16 |
|---|---|
| Arduino Uno R4 WiFi 핀 맵 (1) | 2024.04.30 |
| 오토 키보드 기능 사용법 아두이노 R4 Minima (0) | 2024.04.19 |
| UNO R4 업로드 에러 해결 방법 Failed uploading: uploading error: exit status 74 (1) | 2024.03.26 |
| Arduino UNO R4의 FspTimer 라이브러리 사용법 (1) | 2024.03.26 |
| UNO R4 WiFi 네트워크 예제 (1) | 2024.03.20 |
| 우노 R4 WiFi 자동 완성 키보드로 사용하기 (1) | 2024.03.20 |
| 아두이노 우노 R4 WiFi CAN Bus 꿀팁 (1) | 2024.03.18 |
취업, 창업의 막막함, 외주 관리, 제품 부재!
당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약,
아이디어는 있지만 구현할 기술이 없는 막막함.
우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.
이제 고민을 멈추고, 캐어랩을 만나세요!
코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.
제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!
귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.
캐어랩