element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
element14 presents
  • Challenges & Projects
  • More
element14 presents
Blog How To Build An Arduino Joystick Mouse
  • Blog
  • Forum
  • Documents
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: airbornesurfer
  • Date Created: 22 Mar 2019 1:13 PM Date Created
  • Views 268 views
  • Likes 7 likes
  • Comments 2 comments
  • arduino leonardo
  • project xyberpunk
  • adafruit feather
  • arduino_tutorials
  • xybernaut
  • atmega32u4
  • mouse
  • arduino
  • joystick mouse
Related
Recommended

How To Build An Arduino Joystick Mouse

airbornesurfer
airbornesurfer
22 Mar 2019
Xybernaut Wearable PC

element14 presents  |  AirborneSurfer's VCP Profile |  Project Videos

 

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

 

In this video, we'll use the Arduino Leonardo-compatible Adafruit ATmega32u4 Featherboard to build a joystick-style mouse that can be used with any USB-equipped computer. This will be built into Project Xyberpunk to replace the Trackpoint-style mouse module in the original design.

 

Here's the code, for those of you following along at home:

 

//*****************************************************
//Project Xyberpunk v1.0
//Joystick mouse module
//Feb 15, 2019
//
//CC-BY-SA
//Includes some public domain code by David A Mellis & Tom Igoe
//(I know I don't have to give credit, but I like to when I can)
//
//Matthew Eargle
//Element14 Presents
//element14.com/presents
//AirborneSurfer Productions
//airbornesurfer.com
//
//*****************************************************
#include <Mouse.h>

const int leftButton = 10;
const int rightButton = 11;
const int xPin = A0;
const int yPin = A1;

int xValue = 0;         // the sensor value
int xMin = 1023;        // minimum sensor value
int xMax = 0;           // maximum sensor value
int yValue = 0;
int yMin = 1023;
int yMax = 0;

//Refresh rate between polling intervals, lower is smoother movement
#define REFRESH_RATE 1
static unsigned long lastRefresh = 0;

void setup()
{

  Serial.begin(115200);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    xValue = analogRead(xPin);
    yValue = analogRead(yPin);

    // record the maximum sensor value
    if (xValue > xMax) {
      xMax = xValue;
    }
    if (yValue > yMax) {
      yMax = yValue;
    }

    // record the minimum sensor value
    if (xValue < xMin) {
      xMin = xValue;
    }
    if (yValue < yMin) {
      yMin = yValue;
    }
  }
  // signal the end of the calibration period
  digitalWrite(13, LOW);
  
  Mouse.begin();

}

void loop()
{
  digitalWrite(leftButton, HIGH);
  digitalWrite(rightButton, HIGH);

  int leftClick = digitalRead(leftButton);
  int rightClick = digitalRead(rightButton);

  // left click:
  if (leftClick == LOW) {
    // 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);
    }
  }
  // right click:
  if (rightClick == LOW) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.press(MOUSE_RIGHT);
    }
  }
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.release(MOUSE_RIGHT);
    }
  }

  if ((lastRefresh + REFRESH_RATE) < millis())
  {
    moveMouse();

    lastRefresh = millis();
  }
}

void moveMouse()
{
  int xValue = analogRead(A1);
  int yValue = analogRead(A0);
  int xMove = (((xValue) - 512) / 50);
  int yMove = ((-1*((yValue) - 512) / 50));
  Serial.print("X: ");
  Serial.print(xMove);
  Serial.print(", Y: ");
  Serial.print(yMove);
  Mouse.move(xMove, yMove, 0);
  Serial.println();
}

Anonymous

Top Comments

  • airbornesurfer
    airbornesurfer over 2 years ago in reply to flenger +1

    Change the divisor in line 122 or 123 to adjust the speed of the cursor movement. The larger the divisor, the slower the movement.

Parents
  • flenger
    flenger over 2 years ago

    Hi, can you tell me how to reduce the speed of movement by half?

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Comment
  • flenger
    flenger over 2 years ago

    Hi, can you tell me how to reduce the speed of movement by half?

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Children
  • airbornesurfer
    airbornesurfer over 2 years ago in reply to flenger

    Change the divisor in line 122 or 123 to adjust the speed of the cursor movement. The larger the divisor, the slower the movement.

    • Cancel
    • Up +1 Down
    • Reply
    • More
    • Cancel
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube