目录

Arduino - 键盘串口( Keyboard Serial)

此示例侦听来自串行端口的字节。 收到后,电路板会将击键发送回计算机。 发送的按键比接收的按键高一个,因此如果从串行监视器发送“a”,您将从连接到计算机的板上收到“b”。 “1”将返回“2”,依此类推。

Warning - 使用Keyboard.print()命令时,Leonardo,Micro或Due板接管计算机的键盘。 为确保在使用此功能运行草图时不会失去对计算机的控制,请在调用Keyboard.print()之前设置可靠的控制系统。 此草图设计为仅在电路板通过串行端口接收到字节后发送键盘命令。

组件的要求 (Components Required)

您将需要以下组件 -

  • 1×Arduino Leonardo,Micro或Due board

过程 (Procedure)

只需使用USB线将电路板连接到电脑即可。

键盘串口面包板

草图 (Sketch)

在您的计算机上打开Arduino IDE软件。 用Arduino语言编码将控制你的电路。 单击“新建”打开新的草图文件。

草图

Notes - 您必须在Arduino库文件中包含键盘库。 将键盘库文件复制并粘贴到文件中,并以黄色突出显示名称“libraries”。

Arduino库文件

Arduino代码 (Arduino Code)

/*
   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);
   }
}

Code to Note

编程后,打开串行监视器并发送一个字节。 董事会将通过击键回复,这是一个更高的数字。

结果 (Result)

当您发送一个字节时,电路板将在Arduino IDE串行监视器上以一个更高的键击响应。

↑回到顶部↑
WIKI教程 @2018