W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
此示例監(jiān)聽來自串口的一個字節(jié)。當(dāng)接收到時,電路板發(fā)送一個擊鍵回到計算機(jī)。發(fā)送的擊鍵比接收的擊鍵高一個,因此如果從串口監(jiān)視器發(fā)送“a”,你將從連接到計算機(jī)的電路板接收到“b”。“1”將返回“2”等。
警告 - 當(dāng)你使用 Keyboard.print()命令時,Leonardo,Micro或Due板會接管你計算機(jī)的鍵盤。為確保在使用此功能運(yùn)行草圖時不會失去對計算機(jī)的控制,請在調(diào)用Keyboard.print()之前設(shè)置可靠的控制系統(tǒng)。這個草圖被設(shè)計為只在板通過串口接收到一個字節(jié)后才發(fā)送一個鍵盤命令。
你將需要以下組件:
只需使用USB線將電路板連接到計算機(jī)。
在計算機(jī)上打開Arduino IDE軟件。使用Arduino語言進(jìn)行編碼控制你的電路。通過單擊“New”打開一個新的草圖文件。
注意 - 你必須在Arduino庫文件中包含鍵盤庫。將鍵盤庫文件復(fù)制并粘貼到以下標(biāo)黃色的名為“l(fā)ibraries”的文件中。
/* Keyboard test For the Arduino Leonardo, Micro or Due Reads a byte from the serial port, sends a keystroke back. The sent keystroke is one higher than what's received, e.g. if you send a, you get b, send A you get B, and so forth. The circuit: * none */ #include "Keyboard.h" void setup() { // open the serial port: Serial.begin(9600); // initialize control over the keyboard: Keyboard.begin(); } void loop() { // check for incoming serial data: if (Serial.available() > 0) { // read incoming serial data: char inChar = Serial.read(); // Type the next ASCII value from what you received: Keyboard.write(inChar + 1); } }
一旦開始編程,則打開你的串口監(jiān)視器并發(fā)送一個字節(jié)。電路板將回復(fù)一個擊鍵,這是一個更高的數(shù)字。
當(dāng)你發(fā)送一個字節(jié)時,電路板將會在Arduino IDE串口監(jiān)視器上回復(fù)一個更高數(shù)字的擊鍵。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: