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传感器01.Arduino 湿度传感器

    在本节中,我们将学习如何使用不同的传感器连接我们的Arduino板。我们将讨论以下传感器: 湿度传感器(DHT22) DHT-22(也称为AM2302)是一个数字输出,相对湿度和温度传感器。它使用电容式湿度传感器和热敏电阻来测量周围空气,并在数据引脚上发送数字信号。 在本例中,你将学习如何将此传感器与Arduino UNO一起使用。室温和湿度将打印到串口监视…

    2017年4月20日
    19900
  • Arduino教程30.Arduino delay()函数

    delay()函数的工作方式非常简单。它接受单个整数(或数字)参数。此数字表示时间(以毫秒为单位)。当程序遇到这个函数时,应该等到下一行代码。然而,问题是,delay()函数并不是让程序等待的好方法,因为它被称为阻塞(blocking)函数。 delay()函数语法 delay (ms) ; 其中, ms 是以毫秒为单位暂停的时间(无符号…

    2017年3月29日
    18000
  • Arduino教程23.Arduino switch case语句

    类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。特别是, switch 语句将变量的值与 case 语句中指定的值进行比较。当发现一个case语句的值与变量的值匹配时,运行case语句中的代码。 switch语句使用&nbsp…

    2017年3月24日
    15700
  • Arduino函数库06.Arduino pinMode()

    描述 将指定的引脚配置为输入或输出。有关引脚功能的详细信息,请参阅数字引脚的说明。从Arduino 1.0.1开始,可以使用INPUT_PULLUP模式启用内部上拉电阻。此外,INPUT模式显式禁止内部上拉。 语法 pinMode(pin, mode) 参数 pin:你希望设置模式的引脚的编号mode:INPUT,OUTPUT或INPUT_PULLUP。(有…

    2017年3月31日
    23900
  • Arduino电机控制03.Arduino 步进电机

    步进电机是无刷同步电机,它将完整的旋转分成多个步骤。与无刷直流电机不同,当向其施加固定的直流电压时,它将连续旋转,步进电机以不连续的步进角旋转。 因此,步进电机被制造成具有每转12,24,72,144,180和200的步长,从而产生每步30°,15°,5°,2.5°,2°和1.8°的步进角。步进电机可以有或没有反馈控制。 想象一下在RC飞机上的电机。电机在一…

    2017年4月28日
    29700

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

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