#include <Wire.h>
#include <MPU6050_light.h>
#include <Mouse.h>
MPU6050 mpu(Wire);
unsigned long timer = 0;
int Xang, Yang, Zang;
int leftButtonPin = 7;
int middleButtonPin = 8;
int rightButtonPin = 9;
void setup() {
pinMode(leftButtonPin, INPUT_PULLUP);
pinMode(middleButtonPin, INPUT_PULLUP);
pinMode(rightButtonPin, INPUT_PULLUP);
Wire.begin();
byte status = mpu.begin();
while (status != 0) {}
delay(1000);
mpu.calcOffsets();
Mouse.begin();
// Keyboard.begin(); // Commented out because Keyboard is not used in this code
}
void loop() {
mpu.update();
if ((millis() - timer) > 60) {
// Reading the MPU data
Xang = mpu.getAngleX();
Yang = mpu.getAngleY();
Zang = mpu.getAngleZ();
int leftButtonState = digitalRead(leftButtonPin);
int middleButtonState = digitalRead(middleButtonPin);
int rightButtonState = digitalRead(rightButtonPin);
static bool leftButtonFlag = false;
static bool middleButtonFlag = false;
static bool rightButtonFlag = false;
if (leftButtonState > 0) {
if (leftButtonFlag) {
Mouse.release(MOUSE_LEFT);
leftButtonFlag = false;
}
} else {
Mouse.press(MOUSE_LEFT);
leftButtonFlag = true;
}
if (middleButtonState > 0) {
if (middleButtonFlag) {
Mouse.release(MOUSE_MIDDLE);
middleButtonFlag = false;
}
} else {
Mouse.press(MOUSE_MIDDLE);
middleButtonFlag = true;
}
if (rightButtonState > 0) {
if (rightButtonFlag) {
Mouse.release(MOUSE_RIGHT);
rightButtonFlag = false;
}
} else {
Mouse.press(MOUSE_RIGHT);
rightButtonFlag = true;
}
if ((Yang > 6) || (Yang < -6) || (Xang > 6) || (Xang < -6)) {
if (Yang > 6) {
Yang = map(Yang, 6, 10, 1, 10);
} else if (Yang < -6) {
Yang = map(Yang, -10, -6, -10, -1);
} else if (Xang > 6) {
Xang = map(Xang, 6, 10, 1, 10);
} else if (Xang < -6) {
Xang = map(Xang, -10, -6, -10, -1);
}
Mouse.move(Yang, Xang, 0);
}
timer = millis();
}
}