
- Arduino 教程
- Arduino - 首頁
- Arduino - 概述
- Arduino - 開發板描述
- Arduino - 安裝
- Arduino - 程式結構
- Arduino - 資料型別
- Arduino - 變數與常量
- Arduino - 運算子
- Arduino - 控制語句
- Arduino - 迴圈
- Arduino - 函式
- Arduino - 字串
- Arduino - 字串物件
- Arduino - 時間
- Arduino - 陣列
- Arduino 函式庫
- Arduino - I/O 函式
- Arduino - 高階 I/O 函式
- Arduino - 字元函式
- Arduino - 數學庫
- Arduino - 三角函式
- Arduino 高階應用
- Arduino - Due & Zero
- Arduino - 脈衝寬度調製 (PWM)
- Arduino - 隨機數
- Arduino - 中斷
- Arduino - 通訊
- Arduino - 積體電路 (I2C)
- Arduino - 序列外圍裝置介面 (SPI)
- Arduino 專案
- Arduino - 閃爍 LED
- Arduino - 漸變 LED
- Arduino - 讀取模擬電壓
- Arduino - LED 條形圖
- Arduino - 鍵盤登出
- Arduino - 鍵盤訊息
- Arduino - 滑鼠按鍵控制
- Arduino - 鍵盤序列埠
- Arduino 感測器
- Arduino - 溼度感測器
- Arduino - 溫度感測器
- Arduino - 水位檢測/感測器
- Arduino - PIR 感測器
- Arduino - 超聲波感測器
- Arduino - 連線開關
- 電機控制
- Arduino - 直流電機
- Arduino - 伺服電機
- Arduino - 步進電機
- Arduino 與聲音
- Arduino - 音調庫
- Arduino - 無線通訊
- Arduino - 網路通訊
- Arduino 有用資源
- Arduino - 快速指南
- Arduino - 有用資源
- Arduino - 討論
Arduino - 鍵盤訊息
在這個例子中,當按下按鈕時,一個文字字串將作為鍵盤輸入傳送到電腦。字串報告按鈕被按下的次數。一旦你將程式寫入Leonardo並連線好電路,開啟你喜歡的文字編輯器檢視結果。
警告 − 當你使用 Keyboard.print() 命令時,Arduino 將接管你的電腦鍵盤。為了確保在執行包含此函式的程式時不會失去對電腦的控制,請在呼叫 Keyboard.print() 之前設定可靠的控制系統。此程式包含一個按鈕來切換鍵盤,以便它僅在按下按鈕後執行。
所需元件
你需要以下元件:
- 1 個 麵包板
- 1 個 Arduino Leonardo、Micro 或 Due 開發板
- 1 個 瞬時按鈕
- 1 個 10k 歐姆電阻
步驟
按照電路圖,將元件連線到麵包板,如下圖所示。

程式
在你的電腦上開啟 Arduino IDE 軟體。使用 Arduino 語言進行程式設計來控制你的電路。點選“新建”開啟一個新的程式檔案。

Arduino 程式碼
/* Keyboard Message test For the Arduino Leonardo and Micro, Sends a text string when a button is pressed. The circuit: * pushbutton attached from pin 4 to +5V * 10-kilohm resistor attached from pin 4 to ground */ #include "Keyboard.h" const int buttonPin = 4; // input pin for pushbutton int previousButtonState = HIGH; // for checking the state of a pushButton int counter = 0; // button push counter void setup() { pinMode(buttonPin, INPUT); // make the pushButton pin an input: Keyboard.begin(); // initialize control over the keyboard: } void loop() { int buttonState = digitalRead(buttonPin); // read the pushbutton: if ((buttonState != previousButtonState)&& (buttonState == HIGH)) // and it's currently pressed: { // increment the button counter counter++; // type out a message Keyboard.print("You pressed the button "); Keyboard.print(counter); Keyboard.println(" times."); } // save the current button state for comparison next time: previousButtonState = buttonState; }
程式碼說明
將按鈕的一個端子連線到 Arduino 的 4 號引腳,另一個端子連線到 5V。使用電阻作為下拉電阻,提供接地參考,將其從 4 號引腳連線到地。
程式寫入開發板後,拔掉 USB 資料線,開啟一個文字編輯器並將文字游標放在輸入區域。再次透過 USB 將開發板連線到電腦,然後按下按鈕在文件中寫入內容。
結果
使用任何文字編輯器,它將顯示透過 Arduino 傳送的文字。
廣告