-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMU_Mouse.ino
48 lines (37 loc) · 1.03 KB
/
IMU_Mouse.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <Wire.h>
#include <BluetoothSerial.h>
#include "MPU6050.h"
#include "HIDMouse.h"
BluetoothSerial SerialBT;
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
SerialBT.begin("ESP32_Mouse");
HIDMouse.begin();
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Convert gyroscope values to mouse movements
int mouseX = gx / 131;
int mouseY = gy / 131;
// Map these values to a suitable range for mouse movement
mouseX = map(mouseX, -32768, 32767, -10, 10);
mouseY = map(mouseY, -32768, 32767, -10, 10);
// Simulate mouse movement
HIDMouse.move(mouseX, mouseY);
// Simulate scroll function (for example, using the Z-axis of the gyroscope)
int scrollValue = gz / 131;
scrollValue = map(scrollValue, -32768, 32767, -5, 5);
if (scrollValue != 0) {
HIDMouse.scroll(scrollValue);
}
delay(10);
}