|
| 1 | +/* |
| 2 | + JoystickMouseControl |
| 3 | + |
| 4 | + Controls the mouse from a joystick on an Arduino Leonardo. |
| 5 | + Uses a pushbutton to turn on and off mouse control. |
| 6 | + |
| 7 | + The mouse movement is always relative. This sketch reads |
| 8 | + two analog inputs that range from 0 to 1023 (or less on either end) |
| 9 | + and translates them into ranges of -60 to 60. |
| 10 | + The sketch assumes that the joystick resting values are around the |
| 11 | + middle of the range, but that they vary within a threshold. |
| 12 | + |
| 13 | + WARNING: When you use the Mouse.move() command, the Arduino takes |
| 14 | + over your mouse! Make sure you have control before you use the command. |
| 15 | + This sketch includes a pushbutton to toggle the mouse control state, so |
| 16 | + you can turn on and off mouse control. |
| 17 | + |
| 18 | + created 15 Sept 2011 |
| 19 | + by Tom Igoe |
| 20 | + |
| 21 | + this code is in the public domain |
| 22 | + |
| 23 | + */ |
| 24 | + |
| 25 | +// set pin numbers for switch, joystick axes, and LED: |
| 26 | +const int switchPin = 6; // switch to turn on and off mouse control |
| 27 | +const int xAxis = A1; // joystick X axis |
| 28 | +const int yAxis = A2; // joystick Y axis |
| 29 | +const int ledPin = 5; // Mouse control LED |
| 30 | + |
| 31 | +// parameters for reading the joystick: |
| 32 | +int range = 12; // output range of X or Y movement |
| 33 | +int responseDelay = 2; // response delay of the mouse, in ms |
| 34 | +int threshold = range/4; // resting threshold |
| 35 | +int center = range/2; // resting position value |
| 36 | +int minima[] = { |
| 37 | + 1023, 1023}; // actual analogRead minima for {x, y} |
| 38 | +int maxima[] = { |
| 39 | + 0,0}; // actual analogRead maxima for {x, y} |
| 40 | +int axis[] = { |
| 41 | + xAxis, yAxis}; // pin numbers for {x, y} |
| 42 | +int mouseReading[2]; // final mouse readings for {x, y} |
| 43 | + |
| 44 | + |
| 45 | +boolean mouseIsActive = false; // whether or not to control the mouse |
| 46 | +int lastSwitchState = LOW; // previous switch state |
| 47 | + |
| 48 | +void setup() { |
| 49 | + pinMode(switchPin, INPUT); // the switch pin |
| 50 | + pinMode(ledPin, OUTPUT); // the LED pin |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + // read the switch: |
| 55 | + int switchState = digitalRead(switchPin); |
| 56 | + // if it's changed and it's high, toggle the mouse state: |
| 57 | + if (switchState != lastSwitchState) { |
| 58 | + if (switchState == HIGH) { |
| 59 | + mouseIsActive = !mouseIsActive; |
| 60 | + // turn on LED to indicate mouse state: |
| 61 | + digitalWrite(ledPin, mouseIsActive); |
| 62 | + } |
| 63 | + } |
| 64 | + // save switch state for next comparison: |
| 65 | + lastSwitchState = switchState; |
| 66 | + |
| 67 | +// read and scale the two axes: |
| 68 | + int xReading = readAxis(0); |
| 69 | + int yReading = readAxis(1); |
| 70 | + |
| 71 | +// if the mouse control state is active, move the mouse: |
| 72 | + if (mouseIsActive) { |
| 73 | + Mouse.move(xReading, yReading, 0); |
| 74 | + } |
| 75 | + delay(responseDelay); |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | + reads an axis (0 or 1 for x or y) and scales the |
| 80 | + analog input range to a range from 0 to <range> |
| 81 | +*/ |
| 82 | + |
| 83 | +int readAxis(int axisNumber) { |
| 84 | + int distance = 0; // distance from center of the output range |
| 85 | + |
| 86 | + // read the analog input: |
| 87 | + int reading = analogRead(axis[axisNumber]); |
| 88 | + |
| 89 | +// of the current reading exceeds the max or min for this axis, |
| 90 | +// reset the max or min: |
| 91 | + if (reading < minima[axisNumber]) { |
| 92 | + minima[axisNumber] = reading; |
| 93 | + } |
| 94 | + if (reading > maxima[axisNumber]) { |
| 95 | + maxima[axisNumber] = reading; |
| 96 | + } |
| 97 | + |
| 98 | + // map the reading from the analog input range to the output range: |
| 99 | + reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range); |
| 100 | + |
| 101 | + // if the output reading is outside from the |
| 102 | + // rest position threshold, use it: |
| 103 | + if (abs(reading - center) > threshold) { |
| 104 | + distance = (reading - center); |
| 105 | + } |
| 106 | + |
| 107 | + // the Y axis needs to be inverted in order to |
| 108 | + // map the movemment correctly: |
| 109 | + if (axisNumber == 1) { |
| 110 | + distance = -distance; |
| 111 | + } |
| 112 | + |
| 113 | + // return the distance for this axis: |
| 114 | + return distance; |
| 115 | +} |
0 commit comments