Air Mouse (Alpha)
A simple HID arduino project that allows you to control the mouse with a gyroscope
Components and supplies
1
Arduino Leonardo
12
Jumper wires (generic)
1
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
2
button
Apps and platforms
1
Arduino IDE
Project description
Code
AirMouse0.1
c_cpp
1#include <Wire.h> 2#include <I2Cdev.h> 3#include <MPU6050.h> 4#include <Mouse.h> 5#define LBUT 7 6#define RBUT 6 7 8int xPin = A1; 9int yPin = A0; 10int buttonPin = 1; 11 12int xPosition = 0; 13int yPosition = 0; 14int buttonState = 0; 15MPU6050 mpu; 16int16_t ax, ay, az, gx, gy, gz; 17 18int angleToDistance(int a) 19{ 20 if (a < -80) 21 { 22 return -40; 23 } 24 else if (a < -65) { 25 return -20; 26 } 27 else if (a < -50) { 28 return -10; 29 } 30 else if (a < -15) { 31 return -5; 32 } 33 else if (a < -5) { 34 return -1; 35 } 36 else if (a > 80) { 37 return 40; 38 } 39 else if (a > 65) { 40 return 20; 41 } 42 else if (a > 15) { 43 return 10; 44 } 45 else if (a > 5) { 46 return 1; 47 } 48} 49 50void setup() { 51 // initialize serial communications at 9600 bps: 52 Serial.begin(9600); 53 54 Mouse.begin(); 55 56 pinMode(xPin, INPUT); 57 pinMode(yPin, INPUT); 58 59 //activate pull-up resistor on the push-button pin 60 pinMode(buttonPin, INPUT_PULLUP); 61 62 // For versions prior to Arduino 1.0.1 63 // pinMode(buttonPin, INPUT); 64 // digitalWrite(buttonPin, HIGH); 65 // put your setup code here, to run once: 66 pinMode(LBUT, INPUT); 67 digitalWrite(LBUT, HIGH); 68 pinMode(RBUT, INPUT); 69 digitalWrite(RBUT, HIGH); 70 Wire.begin(); 71 mpu.initialize(); 72 if (!mpu.testConnection()) { 73 while (1); 74 } 75} 76void loop() { 77 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 78 int vx = map(ax, -16000, 16000, 90, -90); 79 int vy = map(ay, -16000, 16000, 90, -90); 80 attachInterrupt(1, keyboard, CHANGE); 81 attachInterrupt(7,fire,HIGH); 82 Mouse.move(angleToDistance(vx), angleToDistance(vy)); 83 if (digitalRead(LBUT) == LOW) { 84 if (!Mouse.isPressed(MOUSE_LEFT)) { 85 Mouse.press(MOUSE_LEFT); 86 } 87 } 88 else { 89 if (Mouse.isPressed(MOUSE_LEFT)) { 90 Mouse.release(MOUSE_LEFT); 91 } 92 } 93 if (digitalRead(RBUT) == LOW) { 94 if (!Mouse.isPressed(MOUSE_RIGHT)) { 95 Mouse.press(MOUSE_RIGHT); 96 } 97 } 98 else { 99 if (Mouse.isPressed(MOUSE_RIGHT)) { 100 Mouse.release(MOUSE_RIGHT); 101 } 102 } 103 delay(20); 104 xPosition = analogRead(xPin); 105 yPosition = analogRead(yPin); 106 buttonState = digitalRead(buttonPin); 107 Serial.print("X: "); 108 Serial.print(xPosition); 109 110 111 Serial.print(" | Y: "); 112 Serial.print(yPosition); 113 Serial.print(" | Button: "); 114 Serial.println(buttonState); 115 116 // delay(100); // add some delay between reads 117 118 119} 120void keyboard() 121{ 122 xPosition = analogRead(xPin); 123 yPosition = analogRead(yPin); 124 buttonState = digitalRead(buttonPin); 125 Serial.print("X: "); 126 Serial.print(xPosition); 127 128 129 Serial.print(" | Y: "); 130 Serial.print(yPosition); 131 Serial.print(" | Button: "); 132 Serial.println(buttonState); 133 134 //delay(100); // add some delay between reads 135 136} 137void fire() 138{ 139 140}
Comments
Only logged in users can leave comments