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.
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.
/* 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