source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/09.USB/Mouse/ButtonMouseControl/ButtonMouseControl.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 2.1 KB
Line 
1
2/*
3  ButtonMouseControl
4
5 For Leonardo and Due boards only.
6
7 Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.
8
9 Hardware:
10 * 5 pushbuttons attached to D2, D3, D4, D5, D6
11
12
13 The mouse movement is always relative. This sketch reads
14 four pushbuttons, and uses them to set the movement of the mouse.
15
16 WARNING:  When you use the Mouse.move() command, the Arduino takes
17 over your mouse!  Make sure you have control before you use the mouse commands.
18
19 created 15 Mar 2012
20 modified 27 Mar 2012
21 by Tom Igoe
22
23 this code is in the public domain
24
25 */
26
27#include "Mouse.h"
28
29// set pin numbers for the five buttons:
30const int upButton = 2;
31const int downButton = 3;
32const int leftButton = 4;
33const int rightButton = 5;
34const int mouseButton = 6;
35
36int range = 5;              // output range of X or Y movement; affects movement speed
37int responseDelay = 10;     // response delay of the mouse, in ms
38
39
40void setup() {
41  // initialize the buttons' inputs:
42  pinMode(upButton, INPUT);
43  pinMode(downButton, INPUT);
44  pinMode(leftButton, INPUT);
45  pinMode(rightButton, INPUT);
46  pinMode(mouseButton, INPUT);
47  // initialize mouse control:
48  Mouse.begin();
49}
50
51void loop() {
52  // read the buttons:
53  int upState = digitalRead(upButton);
54  int downState = digitalRead(downButton);
55  int rightState = digitalRead(rightButton);
56  int leftState = digitalRead(leftButton);
57  int clickState = digitalRead(mouseButton);
58
59  // calculate the movement distance based on the button states:
60  int  xDistance = (leftState - rightState) * range;
61  int  yDistance = (upState - downState) * range;
62
63  // if X or Y is non-zero, move:
64  if ((xDistance != 0) || (yDistance != 0)) {
65    Mouse.move(xDistance, yDistance, 0);
66  }
67
68  // if the mouse button is pressed:
69  if (clickState == HIGH) {
70    // if the mouse is not pressed, press it:
71    if (!Mouse.isPressed(MOUSE_LEFT)) {
72      Mouse.press(MOUSE_LEFT);
73    }
74  }
75  // else the mouse button is not pressed:
76  else {
77    // if the mouse is pressed, release it:
78    if (Mouse.isPressed(MOUSE_LEFT)) {
79      Mouse.release(MOUSE_LEFT);
80    }
81  }
82
83  // a delay so the mouse doesn't move too fast:
84  delay(responseDelay);
85}
Note: See TracBrowser for help on using the repository browser.