Project tutorial

Arduino as a Mouse © LGPL

Control your mouse cursor with this simple Leonardo switch setup.

  • 4,165 views
  • 0 comments
  • 8 respects

Components and supplies

About this project


First, gather everything and connect them according to this schematic. (These button connections.)

Connect the buttons to the Leonardo and upload the sketch.

I've attached the PDF for PCB etching. Just print the PDF on glossy paper and transfer the ink to a blank copper clad board.

Thanks UTSOURCE.net to offer electronic components for this project!

you can check UTSOURCE.net, they deal in all kinds of electronic components, for example, Arduino board, Oled displays, proximity sensors, and many more.

Watch this video for a more detailed tutorial on this topic.


Code

codeC/C++
#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);
}

Custom parts and enclosures

pdf for pcb etching

Schematics

sch for button circuit
Capture scn8vprlpn
sch for leonardo wiring
Image7 z6m4fgbze8

Comments

Similar projects you might like

Arduino Game Controller

Project tutorial by Arnov Sharma and Arnov Sharma

  • 11,307 views
  • 5 comments
  • 17 respects

DIY Arduino Remote for TV

Project tutorial by Arnov Sharma

  • 11,499 views
  • 2 comments
  • 8 respects

How to make Arduino Piano keyboard

Project tutorial by Sharifdeen Ashshak

  • 7,307 views
  • 2 comments
  • 12 respects

Gesture Mouse

by Team ONYX

  • 18,585 views
  • 3 comments
  • 21 respects

ThimbleKrox - Mouse Control with Your Fingers

Project tutorial by Magform

  • 4,508 views
  • 9 comments
  • 18 respects

Getting Started with ILI9255 TFT LCD

Project tutorial by Arnov Sharma

  • 4,464 views
  • 2 comments
  • 4 respects
Add projectSign up / Login