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

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

Adding new version

File size: 3.5 KB
Line 
1/*
2  JoystickMouseControl
3
4 Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due.
5 Uses a pushbutton to turn on and off mouse control, and
6 a second pushbutton to click the left mouse button
7
8 Hardware:
9 * 2-axis joystick connected to pins A0 and A1
10 * pushbuttons connected to pin D2 and D3
11
12 The mouse movement is always relative. This sketch reads
13 two analog inputs that range from 0 to 1023 (or less on either end)
14 and translates them into ranges of -6 to 6.
15 The sketch assumes that the joystick resting values are around the
16 middle of the range, but that they vary within a threshold.
17
18 WARNING:  When you use the Mouse.move() command, the Arduino takes
19 over your mouse!  Make sure you have control before you use the command.
20 This sketch includes a pushbutton to toggle the mouse control state, so
21 you can turn on and off mouse control.
22
23 created 15 Sept 2011
24 updated 28 Mar 2012
25 by Tom Igoe
26
27 this code is in the public domain
28
29 */
30
31#include "Mouse.h"
32
33// set pin numbers for switch, joystick axes, and LED:
34const int switchPin = 2;      // switch to turn on and off mouse control
35const int mouseButton = 3;    // input pin for the mouse pushButton
36const int xAxis = A0;         // joystick X axis
37const int yAxis = A1;         // joystick Y axis
38const int ledPin = 5;         // Mouse control LED
39
40// parameters for reading the joystick:
41int range = 12;               // output range of X or Y movement
42int responseDelay = 5;        // response delay of the mouse, in ms
43int threshold = range / 4;    // resting threshold
44int center = range / 2;       // resting position value
45
46boolean mouseIsActive = false;    // whether or not to control the mouse
47int lastSwitchState = LOW;        // previous switch state
48
49void setup() {
50  pinMode(switchPin, INPUT);       // the switch pin
51  pinMode(ledPin, OUTPUT);         // the LED pin
52  // take control of the mouse:
53  Mouse.begin();
54}
55
56void loop() {
57  // read the switch:
58  int switchState = digitalRead(switchPin);
59  // if it's changed and it's high, toggle the mouse state:
60  if (switchState != lastSwitchState) {
61    if (switchState == HIGH) {
62      mouseIsActive = !mouseIsActive;
63      // turn on LED to indicate mouse state:
64      digitalWrite(ledPin, mouseIsActive);
65    }
66  }
67  // save switch state for next comparison:
68  lastSwitchState = switchState;
69
70  // read and scale the two axes:
71  int xReading = readAxis(A0);
72  int yReading = readAxis(A1);
73
74  // if the mouse control state is active, move the mouse:
75  if (mouseIsActive) {
76    Mouse.move(xReading, yReading, 0);
77  }
78
79  // read the mouse button and click or not click:
80  // if the mouse button is pressed:
81  if (digitalRead(mouseButton) == HIGH) {
82    // if the mouse is not pressed, press it:
83    if (!Mouse.isPressed(MOUSE_LEFT)) {
84      Mouse.press(MOUSE_LEFT);
85    }
86  }
87  // else the mouse button is not pressed:
88  else {
89    // if the mouse is pressed, release it:
90    if (Mouse.isPressed(MOUSE_LEFT)) {
91      Mouse.release(MOUSE_LEFT);
92    }
93  }
94
95  delay(responseDelay);
96}
97
98/*
99  reads an axis (0 or 1 for x or y) and scales the
100 analog input range to a range from 0 to <range>
101 */
102
103int readAxis(int thisAxis) {
104  // read the analog input:
105  int reading = analogRead(thisAxis);
106
107  // map the reading from the analog input range to the output range:
108  reading = map(reading, 0, 1023, 0, range);
109
110  // if the output reading is outside from the
111  // rest position threshold,  use it:
112  int distance = reading - center;
113
114  if (abs(distance) < threshold) {
115    distance = 0;
116  }
117
118  // return the distance for this axis:
119  return distance;
120}
121
122
123
Note: See TracBrowser for help on using the repository browser.