- Arduino - 教程
- Arduino - 概述
- Arduino - Board Description
- Arduino - 安装
- Arduino - 程序结构
- Arduino - 数据类型
- Arduino - Variables & 常量
- Arduino - 运算符
- Arduino - 控制声明( Control Statements)
- Arduino - 循环
- Arduino - 功能( Functions)
- Arduino - 字符串( Strings)
- Arduino - String对象( String Object)
- Arduino - Time
- Arduino - 阵列( Arrays)
- Arduino - I/O Functions
- Arduino - Advanced I/O Function
- Arduino - 字符函数( Character Functions)
- Arduino - 数学图书馆( Math Library)
- Arduino - 三角函数( Trigonometric Functions)
- Arduino - Due & Zero
- Arduino - 脉冲宽度调制( Pulse Width Modulation)
- Arduino - 随机数( Random Numbers)
- Arduino - 中断( Interrupts)
- Arduino - 沟通( Communication)
- Arduino - 内部集成电路( Inter Integrated Circuit)
- Arduino - 串行外设接口( Serial Peripheral Interface)
- Arduino - 闪烁LED( Blinking LED)
- Arduino - 褪色LED( Fading LED)
- Arduino - 读取模拟电压( Reading Analog Voltage)
- Arduino - LED条形图( LED Bar Graph)
- Arduino - 键盘注销( Keyboard Logout)
- Arduino - 键盘消息( Keyboard Message)
- Arduino - 鼠标按钮控制( Mouse Button Control)
- Arduino - 键盘串口( Keyboard Serial)
- Arduino - 湿度传感器( Humidity Sensor)
- Arduino - 温度传感器( Temperature Sensor)
- Arduino - Water Detector / Sensor
- Arduino - PIR传感器( PIR Sensor)
- Arduino - 超声波传感器( Ultrasonic Sensor)
- Arduino - 连接开关( Connecting Switch)
- Arduino - 直流电机( DC Motor)
- Arduino - 伺服电机( Servo Motor)
- Arduino - 步进电机( Stepper Motor)
- Arduino - 音调库( Tone Library)
- Arduino - 无线通信( Wireless Communication)
- Arduino - 网络通信( Network Communication)
- Arduino - 有用的资源
- Arduino - 讨论
- Show Example 1
- Show Example 2
- Show Example 3
- Show Example 4
- Show Example 5
- If statement
- If …else statement
- If…else if …else statement
- switch case statement
- Conditional Operator ? :
- while循环
- do…while循环
- for循环
- Nested循环
- Infinite循环
- delay () function
- delayMicroseconds () function
- millis () function
- micros () function
- 将数组传递给函数(Passing Arrays to Functions)
- 多维数组(Multi-Dimensional Arrays)
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Arduino - 鼠标按钮控制( Mouse Button Control)
使用鼠标库,您可以使用Arduino Leonardo,Micro或Due控制计算机的屏幕光标。
此特定示例使用五个按钮移动屏幕上的光标。 其中四个按钮是方向性的(向上,向下,向左,向右),一个用于鼠标左键单击。 Arduino的光标移动始终是相对的。 每次读取输入时,光标的位置都会相对于其当前位置进行更新。
每当按下一个方向按钮时,Arduino将移动鼠标,将HIGH输入映射到适当方向的5的范围。
第五个按钮用于控制鼠标左键单击。 释放按钮后,计算机将识别该事件。
组件的要求 (Components Required)
您将需要以下组件 -
- 1 × Breadboard
- 1×Arduino Leonardo,Micro或Due board
- 5×10k欧姆电阻器
- 5×瞬时按钮
过程 (Procedure)
按照电路图并连接面包板上的组件,如下图所示。
草图 (Sketch)
在您的计算机上打开Arduino IDE软件。 用Arduino语言编码将控制你的电路。 单击“新建”打开新的草图文件。
对于此示例,您需要使用Arduino IDE 1.6.7
Arduino代码 (Arduino Code)
/*
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);
}
Code to Note
使用micro-USB线将电路板连接到计算机。 按钮连接到引脚2至6的数字输入。确保使用10k下拉电阻。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论