Arduino项目07.Arduino 鼠标按钮控制

使用鼠标库,你可以使用Arduino Leonardo,Micro或Due来控制计算机的屏幕光标。

这个特殊的例子使用五个按钮来移动屏幕上的光标。四个按钮是方向性的(上,下,左,右),一个是用于鼠标左键单击。来自Arduino的光标移动总是相对的。每次读取输入时,光标的位置都会相对于当前位置进行更新。

只要有一个方向按钮被按下,Arduino就会移动鼠标,在合适的方向上将HIGH输入映射到5的范围。

第五个按钮用于控制来自鼠标的左键单击。当按钮被释放时,计算机将识别事件。

必需的组件

你将需要以下组件:

  • 1 × Breadboard 面包板
  • 1 × Arduino Leonardo, Micro 或 Due板
  • 5 × 10k欧姆电阻
  • 5 × 瞬时按钮

程序

按照电路图连接面包板上的组件,如下图所示。

面包板上的组件

草图

在计算机上打开Arduino IDE软件。使用Arduino语言进行编码控制你的电路。通过单击“New”打开一个新的草图文件。

对于本例,你需要使用Arduino IDE 1.6.7

Sketch

Arduino代码

/*
   Button Mouse Control
   For Leonardo and Due boards only .Controls the mouse from 
   five pushbuttons on an Arduino Leonardo, Micro or Due.
   Hardware:
   * 5 pushbuttons attached to D2, D3, D4, D5, D6
   The mouse movement is always relative. This sketch reads
   four pushbuttons, and uses them to set the movement of the mouse.
   WARNING: When you use the Mouse.move() command, the Arduino takes
   over your mouse! Make sure you have control before you use the mouse commands.
*/

#include "Mouse.h"
// set pin numbers for the five buttons:
const int upButton = 2;
const int downButton = 3;
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;
int range = 5; // output range of X or Y movement; affects movement speed
int responseDelay = 10; // response delay of the mouse, in ms

void setup() {
   // initialize the buttons' inputs:
   pinMode(upButton, INPUT);
   pinMode(downButton, INPUT);
   pinMode(leftButton, INPUT);
   pinMode(rightButton, INPUT);
   pinMode(mouseButton, INPUT);
   // initialize mouse control:
   Mouse.begin();
}

void loop() {
   // read the buttons:
   int upState = digitalRead(upButton);
   int downState = digitalRead(downButton);
   int rightState = digitalRead(rightButton);
   int leftState = digitalRead(leftButton);
   int clickState = digitalRead(mouseButton);
   // calculate the movement distance based on the button states:
   int xDistance = (leftState - rightState) * range;
   int yDistance = (upState - downState) * range;
   // if X or Y is non-zero, move:
   if ((xDistance != 0) || (yDistance != 0)) {
      Mouse.move(xDistance, yDistance, 0);
   }

   // if the mouse button is pressed:
   if (clickState == HIGH) {
      // if the mouse is not pressed, press it:
      if (!Mouse.isPressed(MOUSE_LEFT)) {
         Mouse.press(MOUSE_LEFT);
      }
   } else {                           // else the mouse button is not pressed:
      // if the mouse is pressed, release it:
      if (Mouse.isPressed(MOUSE_LEFT)) {
         Mouse.release(MOUSE_LEFT);
      }
   }
   // a delay so the mouse does not move too fast:
   delay(responseDelay);
}

代码说明

使用micro-USB线将电路板连接到计算机。按钮连接到引脚2至6的数字输入。确保使用10k下拉电阻。

发布者:suiyublg,转转请注明出处:https://huibian.net/1407-2/

(0)
suiyublg的头像suiyublg
上一篇 2017年4月17日 16:50
下一篇 2017年4月19日 16:53

相关推荐

  • Arduino教程35.Arduino 多维数组

    具有两个维度(即,下标)的数组通常表示由排列在行和列中的信息组成的值的表格。 以下是多维数组的关键特性: 下图说明了一个二维数组 a 。该数组包含三行四列,因此它是一个3乘4的数组。通常,具有 m 行和 n 列的数组称为 m乘n数组。 数组 a 中的每个元素都由&nbs…

    2017年3月30日
    44300
  • Arduino教程33.Arduino micros()函数

    micros()函数返回Arduino板开始运行当前程序时的微秒数。该数字在大约70分钟后溢出,即回到零。在16 MHz Arduino板(例如Duemilanove和Nano)上,此函数的分辨率为4微秒(即返回值总是4的倍数)。在8 MHz Arduino板(例如LilyPad)上,此函数的分辨率为8微秒。 micros()函数语法 micros () ;…

    2017年3月29日
    38700
  • Arduino项目05.Arduino 键盘注销

    本示例使用键盘库将你在计算机上的用户会话注销,此时ARDUINO UNO上的引脚2被拉至接地。草图同时按两个或三个按键的顺序模拟按键,并在短暂的延迟之后释放它们。 警告 – 当你使用 Keyboard.print()命令时,Arduino接管你的计算机键盘。为确保在使用此功能运行草图时不会失去对计算机的控制,请在调用Keybo…

    2017年4月16日
    49700
  • Arduino进阶07.Arduino 串行外设接口

    串行外设接口(SPI)总线是用于串行通信的系统,最多可使用四个导体,通常为三个。一个导体用于数据接收,一个导体用于数据发送,一个导体用于同步,另一个导体用于选择与之通信的设备。它是一个全双工连接,这意味着数据是同时发送和接收的。最大波特率高于I2C通信系统中的波特率。

    2017年4月11日
    74466
  • Arduino教程01.Arduino 概述

    Arduino是一个基于易用硬件和软件的原型平台(开源)。它由可编程的电路板(称为微控制器)和称为Arduino IDE(集成开发环境)的现成软件组成,用于将计算机代码写入并上传到物理板。

    2017年3月17日
    47302

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
欢迎大家来到大雄学编程!