课程表

Arduino 基础

Arduino 函数库

Arduino 进阶

Arduino 项目

Arduino 传感器

Arduino 电机控制

Arduino 声音

工具箱
速查手册

Arduino 鼠标按钮控制

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

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

此特定示例使用五个按钮来移动屏幕上的光标。 四个按钮是定向的(上,下,左,右),一个是用于鼠标左键单击。 来自Arduino的光标移动总是相对的。 每次读取输入时,相对于其当前位置更新光标的位置。

每当按下一个方向按钮时,Arduino将移动鼠标,将HIGH输入映射到适当方向的范围5。

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

 

必需的组件

您将需要以下组件 -

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

 

程序

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

 

面包板上的组件

 

草图

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

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

 

Sketch

 

Arduino代码

  1. /*
  2. Button Mouse Control
  3. For Leonardo and Due boards only .Controls the mouse from
  4. five pushbuttons on an Arduino Leonardo, Micro or Due.
  5. Hardware:
  6. * 5 pushbuttons attached to D2, D3, D4, D5, D6
  7. The mouse movement is always relative. This sketch reads
  8. four pushbuttons, and uses them to set the movement of the mouse.
  9. WARNING: When you use the Mouse.move() command, the Arduino takes
  10. over your mouse! Make sure you have control before you use the mouse commands.
  11. */
  12.  
  13. #include "Mouse.h"
  14. // set pin numbers for the five buttons:
  15. const int upButton = 2;
  16. const int downButton = 3;
  17. const int leftButton = 4;
  18. const int rightButton = 5;
  19. const int mouseButton = 6;
  20. int range = 5; // output range of X or Y movement; affects movement speed
  21. int responseDelay = 10; // response delay of the mouse, in ms
  22.  
  23. void setup() {
  24. // initialize the buttons' inputs:
  25. pinMode(upButton, INPUT);
  26. pinMode(downButton, INPUT);
  27. pinMode(leftButton, INPUT);
  28. pinMode(rightButton, INPUT);
  29. pinMode(mouseButton, INPUT);
  30. // initialize mouse control:
  31. Mouse.begin();
  32. }
  33.  
  34. void loop() {
  35. // read the buttons:
  36. int upState = digitalRead(upButton);
  37. int downState = digitalRead(downButton);
  38. int rightState = digitalRead(rightButton);
  39. int leftState = digitalRead(leftButton);
  40. int clickState = digitalRead(mouseButton);
  41. // calculate the movement distance based on the button states:
  42. int xDistance = (leftState - rightState) * range;
  43. int yDistance = (upState - downState) * range;
  44. // if X or Y is non-zero, move:
  45. if ((xDistance != 0) || (yDistance != 0)) {
  46. Mouse.move(xDistance, yDistance, 0);
  47. }
  48.  
  49. // if the mouse button is pressed:
  50. if (clickState == HIGH) {
  51. // if the mouse is not pressed, press it:
  52. if (!Mouse.isPressed(MOUSE_LEFT)) {
  53. Mouse.press(MOUSE_LEFT);
  54. }
  55. } else { // else the mouse button is not pressed:
  56. // if the mouse is pressed, release it:
  57. if (Mouse.isPressed(MOUSE_LEFT)) {
  58. Mouse.release(MOUSE_LEFT);
  59. }
  60. }
  61. // a delay so the mouse does not move too fast:
  62. delay(responseDelay);
  63. }

 

代码说明

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

转载本站内容时,请务必注明来自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号