From 69286008d55eb8005bd41fa1a05af79b32dcfc45 Mon Sep 17 00:00:00 2001 From: jeeva2812 Date: Wed, 25 Mar 2020 17:37:00 +0530 Subject: [PATCH] Added examples --- .../ButtonMouseControl/ButtonMouseControl.ino | 84 ++++++++++++ .../JoystickMouseControl.ino | 120 ++++++++++++++++++ .../KeyboardAndMouseControl.ino | 93 ++++++++++++++ 3 files changed, 297 insertions(+) create mode 100644 examples/ButtonMouseControl/ButtonMouseControl.ino create mode 100644 examples/JoystickMouseControl/JoystickMouseControl.ino create mode 100644 examples/KeyboardAndMouseControl/KeyboardAndMouseControl.ino diff --git a/examples/ButtonMouseControl/ButtonMouseControl.ino b/examples/ButtonMouseControl/ButtonMouseControl.ino new file mode 100644 index 0000000..6e4fe18 --- /dev/null +++ b/examples/ButtonMouseControl/ButtonMouseControl.ino @@ -0,0 +1,84 @@ +/* + ButtonMouseControl + + For Leonardo and Due boards only. + + Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due. + + Hardware: + - five pushbuttons attached to D2, D3, D4, D5, D6 + + 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 15 Mar 2012 + modified 27 Mar 2012 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/ButtonMouseControl +*/ + +#include "Mouse.h" + +// 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 = 5; // output range of X or Y movement; affects movement speed +int responseDelay = 10; // 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); + // initialize mouse control: + Mouse.begin(); +} + +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); +} \ No newline at end of file diff --git a/examples/JoystickMouseControl/JoystickMouseControl.ino b/examples/JoystickMouseControl/JoystickMouseControl.ino new file mode 100644 index 0000000..aa608f8 --- /dev/null +++ b/examples/JoystickMouseControl/JoystickMouseControl.ino @@ -0,0 +1,120 @@ +/* + JoystickMouseControl + + Controls the mouse from a joystick on an Arduino Leonardo, Micro or Due. + 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 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 -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. + + WARNING: When you use the Mouse.move() command, the Arduino takes over your + mouse! Make sure you have control before you use the command. This sketch + includes a pushbutton to toggle the mouse control state, so you can turn on + and off mouse control. + + created 15 Sep 2011 + updated 28 Mar 2012 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/JoystickMouseControl +*/ + +#include "Mouse.h" + +// set pin numbers for switch, joystick axes, and LED: +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 = 5; // response delay of the mouse, in ms +int threshold = range / 4; // resting threshold +int center = range / 2; // resting position value + +bool mouseIsActive = false; // whether or not to control the mouse +int lastSwitchState = LOW; // previous switch state + +void setup() { + pinMode(switchPin, INPUT); // the switch pin + pinMode(ledPin, OUTPUT); // the LED pin + // take control of the mouse: + Mouse.begin(); +} + +void loop() { + // read the switch: + int switchState = digitalRead(switchPin); + // if it's changed and it's high, toggle the mouse state: + if (switchState != lastSwitchState) { + if (switchState == HIGH) { + mouseIsActive = !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(A0); + int yReading = readAxis(A1); + + // 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 +*/ + +int readAxis(int thisAxis) { + // read the analog input: + int reading = analogRead(thisAxis); + + // map the reading from the analog input range to the output range: + 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; + } + + // return the distance for this axis: + return distance; +} diff --git a/examples/KeyboardAndMouseControl/KeyboardAndMouseControl.ino b/examples/KeyboardAndMouseControl/KeyboardAndMouseControl.ino new file mode 100644 index 0000000..9713541 --- /dev/null +++ b/examples/KeyboardAndMouseControl/KeyboardAndMouseControl.ino @@ -0,0 +1,93 @@ +/* + KeyboardAndMouseControl + + Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due. + + Hardware: + - five pushbuttons attached to D2, D3, D4, D5, D6 + + 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 15 Mar 2012 + modified 27 Mar 2012 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/KeyboardAndMouseControl +*/ + +#include "Keyboard.h" +#include "Mouse.h" + +// 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; + +void setup() { // initialize the buttons' inputs: + pinMode(upButton, INPUT); + pinMode(downButton, INPUT); + pinMode(leftButton, INPUT); + pinMode(rightButton, INPUT); + pinMode(mouseButton, INPUT); + + Serial.begin(9600); + // initialize mouse control: + Mouse.begin(); + Keyboard.begin(); +} + +void loop() { + // use serial input to control the mouse: + if (Serial.available() > 0) { + char inChar = Serial.read(); + + switch (inChar) { + case 'u': + // move mouse up + Mouse.move(0, -40); + break; + case 'd': + // move mouse down + Mouse.move(0, 40); + break; + case 'l': + // move mouse left + Mouse.move(-40, 0); + break; + case 'r': + // move mouse right + Mouse.move(40, 0); + break; + case 'm': + // perform mouse left click + Mouse.click(MOUSE_LEFT); + break; + } + } + + // use the pushbuttons to control the keyboard: + if (digitalRead(upButton) == HIGH) { + Keyboard.write('u'); + } + if (digitalRead(downButton) == HIGH) { + Keyboard.write('d'); + } + if (digitalRead(leftButton) == HIGH) { + Keyboard.write('l'); + } + if (digitalRead(rightButton) == HIGH) { + Keyboard.write('r'); + } + if (digitalRead(mouseButton) == HIGH) { + Keyboard.write('m'); + } + +}