这篇文章来源于deviceplus.com英语网站的翻译稿。
本文最初发布在deviceplus.jp网站上,而后被翻译成英语。
目录
前言
电子设计步骤
关于arduino pro micro
使之被识别为hid
使用操纵杆创建鼠标设备
结论
相关文章
前言
本文中,我将介绍一种不一样的arduino使用方式。乍一看,照片中的arduino看起来像我们之前系列中使用过的arduino pro mini,但其实这是另一种arduino。它被称为“arduino pro micro”。虽然“mini”变成了“micro”,尺寸却并没有发生任何变化,因此,两者有点难以区分。这种arduino在连接到电脑时会被识别为鼠标或键盘等hid设备。
电子设计步骤
预计完成时间:60分钟
所需元器件
arduino主机(arduino pro micro)
面包板
双轴操纵杆模块#27800
轻触开关
220ω 电阻
led
1. 关于arduino pro micro
arduino pro micro是一种arduino,配备有名为“atmega32u4”的芯片(uno等配有atmega328p等)。该芯片最大的特点是当通过usb连接时会伪装成键盘和鼠标等人机接口设备(hid)。配备atmega32u4的arduino除了“pro micro”之外,还被称为“arduino leonardo”,是非常有名的开发板。
在编写程序时,您可以选择名为“arduino leonardo.”的开发板。
乍一看,arduino pro mini与arduino pro micro的外观非常相似。
但是,pro micro具有可以连接到智能手机等设备的usb连接器,而pro mini只有一个串行连接器。
2. 使之被识别为hid
现在,我们让外观相似的arduino pro micro读取示例程序并尝试让电脑将其识别为hid。
尝试运行arduino ide的“file”-“sketch example”-“09.usb”-“keyboard”-“keyboardmessage”程序。
在这个程序中,我们创建一个在引脚4上设有开关的简单电路,当引脚4被按下时,应通过键盘输入显示按下的次数。
(这次,我将引脚4改换为引脚7)
#include keyboard.h const int buttonpin = 7; // input pin for pushbuttonint previousbuttonstate = high; // for checking the state of a pushbuttonint 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;}
编写程序并打开记事本后,无需触碰键盘,每按一次按钮,就会按照上面的描述进行计数。
如果可以如此轻松地制作usb设备,那么就可以实现更多梦想!
3. 使用操纵杆创建鼠标设备
我们已经知道arduino pro micro可以用作hid,下面我想通过将它与其他一些元器件组合来创建鼠标设备。这一次,我将使用曾经在无线电控制设备制作中使用过的操纵杆,并尝试创建一个可以用操纵杆和轻触开关来代替鼠标的设备。
首先,准备一个可用于设置操纵杆方向的程序。
将电路添加到之前的轻触开关电路中。将操纵杆和后面要使用的led连接到引脚2。
code example
const int _udpin = a0; // ud inputconst int _lrpin = a1; // lr inputconst int _swpin = 7; // digital pinint _ud = 0; // value for up/downint _lr = 0; // value for left/right void setup() { serial.begin(9600); pinmode(_swpin,input) ;} void loop() { _ud = analogread(_udpin); _lr = analogread(_lrpin); serial.print(up-down:); serial.print(_ud, dec); serial.print( - left-rright:); serial.println(_lr, dec); if (digitalread(_swpin) == high) { serial.println(switch on); } delay(100);}
经过确认,可以知道它读取了程序,转动操纵杆时数字会发生变化。
接下来,让我们将操纵杆数字值转换为鼠标坐标。实际上,这个程序也是已经备好的示例程序,所以让我们来用用看。请选择“file”-“sketch example”-“09.usb”-“mouse”-“joystickmousecontrol”。
执行此程序时,会将上下(模拟引脚a2)和左右(模拟引脚a1)的值反映在鼠标坐标上。此外,由于引脚2通过接入5v电源来实现开关功能的,因此可以通过将引脚2与vcc相连或将开关夹在中间的方式来打开/关闭设备。
code example
#include mouse.h // set pin numbers for switch, joystick axes, and led:const int switchpin = 5; // switch to turn on and off mouse controlconst int mousebutton = 7; // input pin for the mouse pushbuttonconst int xaxis = a1; // joystick x axisconst int yaxis = a2; // joystick y axisconst int ledpin = 2; // mouse control led // parameters for reading the joystick:int range = 12; // output range of x or y movementint responsedelay = 5; // response delay of the mouse, in msint threshold = range / 4; // resting thresholdint center = range / 2; // resting position value boolean mouseisactive = false; // whether or not to control the mouseint lastswitchstate = low; // previous switch state void setup() {pinmode(switchpin, input); // the switch pinpinmode(ledpin, output); // the led pin// take control of the mouse:mouse.begin();} void loop() {// read the switch:int switchstate = digitalread(switchpin);// if it's changed and it's high, toggle the mouse state:if (switchstate != lastswitchstate) {if (switchstate == high) {mouseisactive = !mouseisactive;// turn on led to indicate mouse state:digitalwrite(ledpin, mouseisactive);}}// save switch state for next comparison:lastswitchstate = switchstate; // read and scale the two axes:int xreading = readaxis(a0);int yreading = readaxis(a1); // if the mouse control state is active, move the mouse:if (mouseisactive) {mouse.move(xreading, yreading, 0);} // read the mouse button and click or not click:// if the mouse button is pressed:if (digitalread(mousebutton) == high) {// if the mouse is not pressed, press it:if (!mouse.ispressed(mouse_left)) {mouse.press(mouse_left);}}// else the mouse button is not pressed:else {// if the mouse is pressed, release it:if (mouse.ispressed(mouse_left)) {mouse.release(mouse_left);}} delay(responsedelay);} /*reads an axis (0 or 1 for x or y) and scales theanalog input range to a range from 0 to */ int readaxis(int thisaxis) {// read the analog input:int reading = analogread(thisaxis); // map the reading from the analog input range to the output range:reading = map(reading, 0, 1023, 0, range); // if the output reading is outside from the// rest position threshold, use it:int distance = reading - center; if (abs(distance) < threshold) {distance = 0;} // return the distance for this axis:return distance;}
完成编程后,我们来尝试让它动起来。
哦,它真的动起来了!
结论
这次,我们学习了使用arduino pro micro创建基于arduino的usb设备时的基本流程。在下一篇文章中,我们将进一步深化应用arduino pro micro,尝试创建更具“device plus”风格的usb设备,让项目更具挑战性!
华为nova8 Pro的体验,妥妥的一款自拍神器
电荷泵与图像内容的PWM输入并联背光驱动ADP8870
红外热成像仪解决卡车轮胎分离的问题分析
电磁制动电动机的工作原理
深圳发放1000万元大红包,你“中签”了吗?
可以用Arduino来制作USB设备吗?尝试通过Arduino Pro Micro(Leonardo)使用HID功能
SPI协议概括
英特尔VR直播 让你甘薯现场看球的热情
村田继续在MLCC产业追加投资,能否扭转局面?
区块链有哪一些基本内容
小米上亿资产遭冻结 申诉被印度驳回 小米回应等待书面判决
IGBT供不应求,行业持续向好发展
以粒子滤波为基础的多信息融合室内定位方法设计详解
市场掀起区块链连锁革命 全球大佬是什么态度?
数字孪生水电站,三维组态助力发电流程优化
接地铜排安裝常见问题?
为什么许多工厂不普及机器人代替产线工人呢?
锡膏的回流焊接需要注意哪些问题
比特币混币的基本知识及使用技巧
数字图像空域滤波算法的FPGA设计与实现