当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

Arduino 键盘串口

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-24 10:05:18

由 drbear 创建,youj 最后一次修改 2016-12-26 此示例监听来自串口的一个字节。当接收到时,电路板发送一个击键回到计算机。发送的击键比接收的击键高一个,因此如果从串口监视器发送“a”,你将从连接到计算机的电路板接收到“b”。“1”将返回“2”等。警告 - 当你使用 Keyboard.print()命令时,Leonardo,Micro或Due板会接管你计算机的键盘。为确保在使用此功能运行草图时不会失去对计算机的控制,请在调用Keyboard.print()之前设置可靠的控制系统。这个草图被设计为只在板通过串口接收到一个字节后才发送一个键盘命令。必需的组件你将需要以下组件: 1 × Arduino Leonardo, Micro, 或 Due板程序只需使用USB线将电路板连接到计算机。草图在计算机上打开Arduino IDE软件。使用Arduino语言进行编码控制你的电路。通过单击“New”打开一个新的草图文件。注意 - 你必须在Arduino库文件中包含键盘库。将键盘库文件复制并粘贴到以下标黄色的名为“libraries”的文件中。Arduino代码/* 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); }}代码说明一旦开始编程,则打开你的串口监视器并发送一个字节。电路板将回复一个击键,这是一个更高的数字。结果当你发送一个字节时,电路板将会在Arduino IDE串口监视器上回复一个更高数字的击键。

Arduino 键盘串口