blazejlo
Published © GPL3+

Gyro-mouse

Moving your mouse with an gyroscope

BeginnerShowcase (no instructions)248
Gyro-mouse

Things used in this project

Hardware components

Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×3
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
×1
Jumper wires (generic)
Jumper wires (generic)
×15
Arduino Micro
Arduino Micro
*Pro micro
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

schema

Code

Untitled file

Arduino
#include <Wire.h>
#include "Mouse.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
float y;
float x;
int mode;
void setup() {
  mode=0;
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  Serial.begin(115200);
 // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }
  Serial.println("");
  delay(100);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
x=-g.gyro.z*2.5;
y=-g.gyro.x*2.5;
if(!digitalRead(4)&& mode==0){mode=1;}
else if(!digitalRead(4)&& mode==1){mode=0;}
while(!digitalRead(4)){delay(150);}
if(mode==1){
Mouse.move(x, y, 0);}
if (!digitalRead(6)) {Mouse.press(MOUSE_LEFT);}
else {Mouse.release(MOUSE_LEFT);}
if (!digitalRead(5)) {Mouse.press(MOUSE_RIGHT);}
else {Mouse.release(MOUSE_RIGHT);}
delay(1/21);
}

Credits

blazejlo

blazejlo

1 project • 0 followers

Comments