Utkarsh TiwariVidit ShahMohinish Sharma
Published © GPL3+

Gesture Mouse

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

IntermediateProtip2 hours19,777
Gesture Mouse

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1
GY-521 MPU-6050 3 Axis Gyroscope + Accelerometer Module For Arduino
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Adafruit Joystick
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

AirMouse

A fritzing file with schematics and code.

Code

AirMouse

Arduino
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()
{
  
}

Credits

Utkarsh Tiwari

Utkarsh Tiwari

10 projects • 40 followers
Trying to FIND myself! :P Interested in DIY's and coding!
Vidit Shah

Vidit Shah

10 projects • 39 followers
Programmer
Mohinish Sharma

Mohinish Sharma

3 projects • 2 followers

Comments