课程表

Arduino 基础

Arduino 函数库

Arduino 进阶

Arduino 项目

Arduino 传感器

Arduino 电机控制

Arduino 声音

工具箱
速查手册

Arduino 键盘串口

当前位置:免费教程 » 程序设计 » Arduino

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

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

 

必需的组件

您将需要以下组件 -

  • 1 × Arduino Leonardo, Micro, 或 Due board
 

程序

只需使用USB电缆将电路板连接到计算机。

 

用USB电缆将电路板连接到计算机

 

草图

在计算机上打开Arduino IDE软件。 在Arduino语言编码将控制你的电路。 通过单击新建打开一个新的草图文件。

 

Sketch

 

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

 

在Arduino库文件中包含键盘库

 

Arduino代码

  1. /*
  2. Keyboard test
  3. For the Arduino Leonardo, Micro or Due Reads
  4. a byte from the serial port, sends a keystroke back.
  5. The sent keystroke is one higher than what's received, e.g. if you send a, you get b, send
  6. A you get B, and so forth.
  7. The circuit:
  8. * none
  9. */
  10.  
  11. #include "Keyboard.h"
  12.  
  13. void setup() {
  14. // open the serial port:
  15. Serial.begin(9600);
  16. // initialize control over the keyboard:
  17. Keyboard.begin();
  18. }
  19.  
  20. void loop() {
  21. // check for incoming serial data:
  22. if (Serial.available() > 0) {
  23. // read incoming serial data:
  24. char inChar = Serial.read();
  25. // Type the next ASCII value from what you received:
  26. Keyboard.write(inChar + 1);
  27. }
  28. }

 

代码说明

一旦编程,打开你的串口监视器并发送一个字节。 板子将回复一个击键,这是一个更高的数字。

 

结果

当你发送一个字节时,板子将以一个高于Arduino IDE串行监视器的数字的键击来回复。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号