Implementation of Joystick Mouse Control

Hi all. I’m trying to control the mouse cursor by using a joystick, but after I uploaded the code into the Arduino Genuino Micro board, my mouse cursor ran out of control. Fyi I adopted the code from the tutorials: I commented out the mouseButton and ledPin related functions, as I do not need them. Is there any way to fix the problem?Thank you.

#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

boolean 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:

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)) {;
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {


  reads an axis (0 or 1 for x or y) and scales the
 analog input range to a range from 0 to <range>

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;

Mouse.move(xReading, yReading, 0);

Well, you are moving the mouse with your code which should therefore be no surprise that the cursor moves (albeit not in the way you wanted!)

Try writing a really simple sketch using the mouse control library just to move it around (eg left a couple of pixels) when you short out an input digital pin to ground - that will give you a basic understanding in how it all works without it going mad.

And put a couple of Serial.println statements in your code so you can see when/why the mouse is moving too.

If you have no control over the motion I would guess that the joystick is wired incorrectly. If you display xReading and yReading on Serial Monitor do you get the expected values?