From 15bd28d495978cfa3c43b25ee362b9af7bf4ed5e Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 9 Mar 2012 16:22:58 -0500 Subject: [PATCH] Added ButtonMouseControl example for Leonardo --- .../ButtonMouseControl/ButtonMouseControl.ino | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino diff --git a/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino b/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino new file mode 100644 index 000000000..bb798f8ac --- /dev/null +++ b/build/shared/examples/10.Mouse/ButtonMouseControl/ButtonMouseControl.ino @@ -0,0 +1,77 @@ +/* + ButtonMouseControl + + Controls the mouse from five pushbuttons on an Arduino Leonardo. + + Hardware: + * 5 pushbuttons attached to D2, D3, D4, D5 + + + 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. + + created 9 Mar 2012 + by Tom Igoe + + this code is in the public domain + + */ + +// 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 = 12; // output range of X or Y movement +int responseDelay = 6; // 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); +} + +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 the mouse button is not pressed: + else { + // if the mouse is pressed, release it: + if (Mouse.isPressed(MOUSE_LEFT)) { + Mouse.release(MOUSE_LEFT); + } + } + + // a delay so the mouse doesn't move too fast: + delay(responseDelay); +}