Gesture Mouse

Gesture Mouse © GPL3+

It's a Gyro+Accelerometer controlled mouse with a button. Made using Leonardo,Gesture Mouse takes CounterStrike to a whole new level!

  • 8,872 views
  • 0 comments
  • 17 respects

Components and supplies

Apps and online services

About this project

1. Leonardo:

Arduino Leonardo is an old board but the only compatible board for this project. It has the required SCL and SDA pin for connection of MPU6050.

Connections: Connect the 5V and GND to breadboard.

2. Joystick:

Use: We use the joystick as a keyboard. While playing a game,we use it move the character in front or back or sideways,etc.

Connections: Connect the GND and 5V in the same line as that of Leonardo as shown in the schematics. Connect the VRX to A1 (Analog Pin 1) and VRY to A0 of Leonardo.

3. MPU6050:

Use: The MPU6050 is a 6 DOF (Degrees of Freedom) or a six axis IMU sensor, which means that it gives six values as output. Three values from the accelerometer and three from the gyroscope. It is basically used as a mouse.

Connections: Connect the GND and VCC in the railing as in schematics. Connect the SCL and SDA to SCL and SDA of Leonardo.

4. Button:

Use: Used as a fire button while playing any game.

Connections: Connect the button pin to Digital Pin 1 and other to GND.

Connect everything according to the schematics. Upload the code to Leonardo and watch the fun!

Note: To switch on the joystick, press it down once.

Code

AirMouseArduino
AirMouse code
#include <Keyboard.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>
#define LBUT 7
#define RBUT 5

int xPin = A1;
int yPin = A0;
int buttonPin = 1;

int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;

int angleToDistance(int a)
{
  if (a < -80)
  {
    return -40;
  }
  else if (a < -65) {
    return -20;
  }
  else if (a < -50) {
    return -10;
  }
  else if (a < -15) {
    return -5;
  }
  else if (a < -5) {
    return -1;
  }
  else if (a > 80) {
    return 40;
  }
  else if (a > 65) {
    return 20;
  }
  else if (a > 15) {
    return 10;
  }
  else if (a > 5) {
    return 1;
  }
}

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  Keyboard.begin();
  Mouse.begin();

  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

  //activate pull-up resistor on the push-button pin
  pinMode(buttonPin, INPUT_PULLUP);

  // For versions prior to Arduino 1.0.1
  // pinMode(buttonPin, INPUT);
  // digitalWrite(buttonPin, HIGH);
  // put your setup code here, to run once:
  pinMode(LBUT, INPUT);
  digitalWrite(LBUT, HIGH);
  pinMode(RBUT, INPUT);
  digitalWrite(RBUT, HIGH);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    while (1);
  }
}
void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  int vx = map(ax, -16000, 16000, 90, -90);
  int vy = map(ay, -16000, 16000, 90, -90);
  attachInterrupt(1, keyboard, CHANGE);
  attachInterrupt(7,fire,HIGH);
  Mouse.move(angleToDistance(vx), angleToDistance(vy));
  if (digitalRead(LBUT) == LOW) {
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  }
  else {
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }
  if (digitalRead(RBUT) == LOW) {
    if (!Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.press(MOUSE_RIGHT);
    }
  }
  else {
    if (Mouse.isPressed(MOUSE_RIGHT)) {
      Mouse.release(MOUSE_RIGHT);
    }
  }
  delay(20);
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  Serial.print("X: ");
  Serial.print(xPosition);


  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);

 // delay(100); // add some delay between reads


  if (yPosition < 10)
  {
    Keyboard.releaseAll();
    Keyboard.press('w'); //for w
    Keyboard.releaseAll();
  }
  if (yPosition < 700 && yPosition > 600||yPosition>1020)
  {
    Keyboard.releaseAll();
    Keyboard.press('s'); //for s
    Keyboard.releaseAll();

  }

  if (xPosition < 10)
  {

    Keyboard.releaseAll();

    Keyboard.press('a'); //for a
    Keyboard.releaseAll();
  }


  if (xPosition < 700 && xPosition > 600||xPosition>1020)
  {
    Keyboard.releaseAll();

    Keyboard.press('d'); //for d
    Keyboard.releaseAll();
  }
}
void keyboard()
{
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  Serial.print("X: ");
  Serial.print(xPosition);


  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);

  //delay(100); // add some delay between reads


  if (yPosition < 10)
  {
    Keyboard.releaseAll();
    Keyboard.press('w'); //for w
    Keyboard.releaseAll();
  }
  if (yPosition < 700 && yPosition > 600||yPosition>1020)
  {
    Keyboard.releaseAll();
    Keyboard.press('s'); //for s
    Keyboard.releaseAll();

  }

  if (xPosition < 10)
  {

    Keyboard.releaseAll();

    Keyboard.press('a'); //for a
    Keyboard.releaseAll();
  }


  if (xPosition < 700 && xPosition > 600||xPosition>1020)
  {
    Keyboard.releaseAll();
    Keyboard.press('d'); //for d
    Keyboard.releaseAll();

  }
}
void fire()
{
  
}

Schematics

AirMouse
A fritzing file with schematics and code.
AirMouse.fzz

Comments

Similar projects you might like

Arduino External Mouse Buttons

by fmattiussi

  • 748 views
  • 4 comments
  • 3 respects

Gesture Controlled Robotic Arm Using Kinect & Arduino

Project showcase by Avinash Baranitharan and Karthikeyan

  • 8,630 views
  • 0 comments
  • 28 respects

Gesture Based Wireless Gaming Console

Project tutorial by Team Xolcano Technologies

  • 1,495 views
  • 0 comments
  • 1 respect

Android App-Based Home Automation System Using IOT

Project tutorial by Team Autoshack

  • 24,826 views
  • 17 comments
  • 75 respects

Digital 3D Printer Filament Counter Using PS/2 Mouse

Project in progress by ArtSuzhou

  • 8,696 views
  • 11 comments
  • 33 respects

Arduino MKR1000 Weather Station

Project tutorial by Tadeas Dvoracek

  • 8,755 views
  • 4 comments
  • 22 respects
Add projectSign up / Login