diff --git a/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino b/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino index bb798f8ac..2c3619f7a 100644 --- a/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino +++ b/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino @@ -4,7 +4,7 @@ Controls the mouse from five pushbuttons on an Arduino Leonardo. Hardware: - * 5 pushbuttons attached to D2, D3, D4, D5 + * 5 pushbuttons attached to D2, D3, D4, D5, D6 The mouse movement is always relative. This sketch reads @@ -13,7 +13,7 @@ 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. - created 9 Mar 2012 + created 15 Mar 2012 by Tom Igoe this code is in the public domain @@ -27,8 +27,8 @@ const int leftButton = 4; const int rightButton = 5; const int mouseButton = 6; -int range = 12; // output range of X or Y movement -int responseDelay = 6; // response delay of the mouse, in ms +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() { diff --git a/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino b/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino index 384597904..16bb98414 100644 --- a/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino +++ b/build/shared/examples/10.Mouse/JoystickMouseControl/JoystickMouseControl.ino @@ -2,15 +2,16 @@ JoystickMouseControl Controls the mouse from a joystick on an Arduino Leonardo. - Uses a pushbutton to turn on and off mouse control. + Uses a pushbutton to turn on and off mouse control, and + a second pushbutton to click the left mouse button Hardware: - * 2-axis joystick connected to pins A1 and A2 - * pushbutton connected to pin D6 + * 2-axis joystick connected to pins A0 and A1 + * pushbuttons connected to pin D2 and D3 The mouse movement is always relative. This sketch reads two analog inputs that range from 0 to 1023 (or less on either end) - and translates them into ranges of -60 to 60. + and translates them into ranges of -6 to 6. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold. @@ -20,7 +21,7 @@ you can turn on and off mouse control. created 15 Sept 2011 - updated 16 Jan 2012 + updated 15 Mar 2012 by Tom Igoe this code is in the public domain @@ -28,24 +29,17 @@ */ // set pin numbers for switch, joystick axes, and LED: -const int switchPin = 6; // switch to turn on and off mouse control -const int xAxis = A1; // joystick X axis -const int yAxis = A2; // joystick Y axis +const int switchPin = 2; // switch to turn on and off mouse control +const int mouseButton = 3; // input pin for the mouse pushButton +const int xAxis = A0; // joystick X axis +const int yAxis = A1; // joystick Y axis const int ledPin = 5; // Mouse control LED // parameters for reading the joystick: int range = 12; // output range of X or Y movement -int responseDelay = 2; // response delay of the mouse, in ms +int responseDelay = 5; // response delay of the mouse, in ms int threshold = range/4; // resting threshold int center = range/2; // resting position value -int minima[] = { - 1023, 1023}; // actual analogRead minima for {x, y} -int maxima[] = { - 0,0}; // actual analogRead maxima for {x, y} -int axis[] = { - xAxis, yAxis}; // pin numbers for {x, y} -int mouseReading[2]; // final mouse readings for {x, y} - boolean mouseIsActive = false; // whether or not to control the mouse int lastSwitchState = LOW; // previous switch state @@ -62,59 +56,63 @@ void loop() { if (switchState != lastSwitchState) { if (switchState == HIGH) { mouseIsActive = !mouseIsActive; - // turn on LED to indicate mouse state: - digitalWrite(ledPin, mouseIsActive); + // turn on LED to indicate mouse state: + digitalWrite(ledPin, mouseIsActive); } } // save switch state for next comparison: lastSwitchState = switchState; -// read and scale the two axes: - int xReading = readAxis(0); - int yReading = readAxis(1); + // read and scale the two axes: + int xReading = readAxis(A0); + int yReading = readAxis(A1); -// if the mouse control state is active, move the mouse: + // if the mouse control state is active, move the mouse: if (mouseIsActive) { Mouse.move(xReading, yReading, 0); } + + // read the mouse button and click or not click: + // if the mouse button is pressed: + if (digitalRead(mouseButton) == HIGH) { + // if the mouse is not pressed, press it: + if (!Mouse.isPressed(MOUSE_LEFT)) { + Mouse.press(MOUSE_LEFT); + } + } + // else the mouse button is not pressed: + else { + // if the mouse is pressed, release it: + if (Mouse.isPressed(MOUSE_LEFT)) { + Mouse.release(MOUSE_LEFT); + } + } + delay(responseDelay); } /* reads an axis (0 or 1 for x or y) and scales the - analog input range to a range from 0 to -*/ + analog input range to a range from 0 to + */ -int readAxis(int axisNumber) { - int distance = 0; // distance from center of the output range - +int readAxis(int thisAxis) { // read the analog input: - int reading = analogRead(axis[axisNumber]); + int reading = analogRead(thisAxis); -// of the current reading exceeds the max or min for this axis, -// reset the max or min: - if (reading < minima[axisNumber]) { - minima[axisNumber] = reading; - } - if (reading > maxima[axisNumber]) { - maxima[axisNumber] = reading; - } - // map the reading from the analog input range to the output range: - reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range); - - // if the output reading is outside from the - // rest position threshold, use it: - if (abs(reading - center) > threshold) { - distance = (reading - center); + reading = map(reading, 0, 1023, 0, range); + + // if the output reading is outside from the + // rest position threshold, use it: + int distance = reading - center; + + if (abs(distance) < threshold) { + distance = 0; } - - // the Y axis needs to be inverted in order to - // map the movemment correctly: - if (axisNumber == 1) { - distance = -distance; - } - + // return the distance for this axis: return distance; } + +