Arduino内置教程-USB-键盘发消息

释放双眼,带上耳机,听听看~!
在这个例子里,当按键按下,一个文本字符串就会像键盘输入那样发送到电脑。这个字符串报告了按键按下的次数。一旦你编译了Leonardo,且连好线,打开你的文本编译器来看结果。

简介

  • 在这个例子里,当按键按下,一个文本字符串就会像键盘输入那样发送到电脑。这个字符串报告了按键按下的次数。一旦你编译了Leonardo,且连好线,打开你的文本编译器来看结果。
  • 注意:当你用 Keyboard.print() 命令时,Arduino会接管你的电脑键盘!为了确保你没有失去对电脑的控制同时运行这个函数,确定在你调用 Keyboard.print()前,启动一个可靠的控制系统。这个程序被设计成只有在一个引脚下拉到地才能发送键盘命令。

硬件要求

  • Arduino Leonardo, Micro, or Due开发板
  • 即时按键
  • 10kΩ电阻

软件要求

  • Arduino IDE

电路

  • 连接按键的一个引脚到开发板的pin4上。连接另一个引脚到5V电源。从pin4到地,增加一个下拉电阻。
  • 一旦你编译好你的开发板,插上USB线,打开文本编译器,并且把文本光标移到打字区域。重新通过USB连接你开发板到电脑里,并按下按键来写入文件。

Arduino内置教程-USB-键盘发消息

Arduino内置教程-USB-键盘发消息

样例代码

/*
 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

 created 24 Oct 2011
 modified 27 Mar 2012
 by Tom Igoe
 modified 11 Nov 2013
 by Scott Fitzgerald

 This example code is in the public domain.

 */

#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() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed,
  if ((buttonState != previousButtonState)
      // and it's currently pressed:
      && (buttonState == HIGH)) {
    // 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;
}

给TA打赏
共{{data.count}}人
人已打赏
ArduinoArduino-官方内置动态

Arduino内置教程-USB-键盘注销

2019-1-6 17:03:43

ArduinoArduino-官方内置动态

Arduino内置教程-USB-键盘再编译

2019-1-6 19:03:43

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'); })();