Arduino 教程--第三十三课 Arduino 鼠标按钮控制
更新时间:2023-10-04 04:31:01 阅读量: 综合文库 文档下载
- arduino推荐度:
- 相关推荐
第三十三课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
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 \// 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下拉电阻。
正在阅读:
Arduino 教程--第三十三课 Arduino 鼠标按钮控制10-04
南京工业大学钢结构简答题10-01
轻生活餐饮空间设计论文资料07-20
班级体创建04-27
全国高考文科综合地理评分细则04-10
华南师范大学《学校德育》在线作业题库(16)及满分答案10-22
2010年第五届卡西欧杯全国高中青年教师优秀课观摩与评比活动教案03-29
采购结算协议11-13
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- Arduino
- 按钮
- 鼠标
- 控制
- 教程
- 三十三
- 关于垃圾焚烧厂的经济补偿问题 - 图文
- 体育赛事的组织与管理考试大纲
- 新闻编辑笔记整理
- 20000最常用美语单词
- 重庆科技学院 - 某油田联合站设计
- 论建筑工程施工质量管理与控制的有效措施
- 湖北省黄冈中学高三物理考前规范训练(1)
- 学生公寓施工组织设计
- 重庆交通大学考研桥梁工程复试题汇总 - 图文
- 把握命题脉搏,展望2006年高考命题趋势
- 尔雅通识-人人爱设计答案(1)
- 王阳明“致良知”说的工夫及现实意义与价值
- 会计基础 第5章练习题及答案
- 公园作文之查询写兰州市红古区海石湾公园的作文500字
- 恋爱心理的文献综述
- 产品成本计算的基本方法 - 分批法
- VC++(MFC)编程(doli等控制器使用)高精度双油缸同步控制设计 - 图文
- 光伏运行人员考试题带答案
- 新理念网络学习大厅综合测试第四册 Unit2
- 小宝宝活动方案