Arduino Joystick Mouse Control Code

Arduino Joystick Mouse Control

Using the Mouse library, you can controls a computer’s onscreen cursor with an Arduino Leonardo, Micro, or Due. This particular example uses a pushbutton to turn on and off mouse control with a joystick.

Arduino Joystick Mouse Control

Cursor movement from the Arduino is always relative. So every time the analog input is read, the cursor’s position is updated relative to it’s current position.

Two analog inputs ranging from 0 to 1023 are translated to ranges of -12 to 12. The sketch assumes that the joystick resting values are around the middle of the range, but that they vary within a threshold.

The pushbutton allows you to toggle mouse control on and off. A status LED connected to pin 5 lights when the Arduino is controlling the mouse.

Software Required

  • none

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

Arduino Joystick Mouse Control Schematic

Code

/*
JoystickMouseControl

Controls the mouse from a joystick on an Arduino Leonardo or Micro.
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

Read more: Arduino Joystick Mouse Control Code

Leave a Comment

Your email address will not be published. Required fields are marked *