Skip to content

Instantly share code, notes, and snippets.

@mandyRae
Last active June 17, 2017 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandyRae/b53d7a430329d18b474f to your computer and use it in GitHub Desktop.
Save mandyRae/b53d7a430329d18b474f to your computer and use it in GitHub Desktop.
This is code to create a computer mouse using an analog joystick and computer mouse.
/*
* Analog Joystick Mouse Emulator with Arduino Micro
*
* This code turns a joystick into a computer mouse.
*
* Written by Amanda on Electrothoughts Jan 2016, with help from:
* - https://www.arduino.cc/en/Tutorial/JoystickMouseControl
* - book Exploring Arduino by Jeremy Blum: https://github.com/sciguy14/Exploring-Arduino/blob/master/Chapter%2006/mouse/mouse.ino
*
* For build instructions and background info visit www.electrothoughts.wordpress.com/2016/01/18/mouse-emulation-with-an-analog-joystick-and-arduino/
*
* Components Required:
* -1 analog joystick
* -1 LED
* -2 tactile pushbuttons
* -1 slide or toggle switch
* -5 330 ohm resistors
*/
#include <Mouse.h>
const int LED1 = 5; //The LED indicates whether the mouse is on and activated
const int TOGGLESWITCH = 13; //Slide or toggle switch activates mouse
const int LEFT_CLICK = 8;
const int RIGHT_CLICK = 2;
const int x_axis = 0; //x-axis of the joystick on A0
const int y_axis = 1; //y-axis of the joystick on A1
void setup() {
Mouse.begin(); //Initialize Mouse
pinMode(LED1, OUTPUT);
pinMode(TOGGLESWITCH, INPUT);
pinMode(LEFT_CLICK, INPUT);
pinMode(RIGHT_CLICK, INPUT);
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
}
void loop() {
while (digitalRead(TOGGLESWITCH) == HIGH) { //While the switch is on, the mouse will work
digitalWrite(LED1, HIGH); //Turn the LED on when the mouse is on
int x_val = readJoystick(x_axis); //Read the joystick values
int y_val = readJoystick(y_axis);
Mouse.move(x_val, y_val, 0); //Move the mouse using the values
readClick(LEFT_CLICK, MOUSE_LEFT); //Check to see if left mouse button is clicked
readClick(RIGHT_CLICK, MOUSE_RIGHT); //Check to see if right mouse button is clicked
delay(10); //This delay adjusts the speed of the mouse; play with the value
}
digitalWrite(LED1, LOW); //Shut the LED off if the joystick mouse isn't active
}
//This function reads each axis of the joystick and adjusts them to usable values
int readJoystick(int axis){
float value = map(analogRead(axis), 0, 1023, -10, 10); //Read axis and map to -10 to 10
if (value <= 1 && value >= -1){ //This conditional adjusts responsiveness, so if the joystick
return 0; //only moves a little, it will return a value of zero, 1 is a good value
}
else { //Return current value of joystick
return value;
}
}
//This function reads when a button is clicked
void readClick(int pin, char mouse_command){
if (digitalRead(pin) == LOW){ //If the button is pressed, and
if (!Mouse.isPressed(mouse_command)){ //if the button wasn't already clicked,
Mouse.press(mouse_command); //click the mouse.
}
}
else{ //Otherwise,
if (Mouse.isPressed(mouse_command)){ //if the button is pressed,
Mouse.release(mouse_command); //release the click.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment