-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARDUINO CODE
More file actions
71 lines (57 loc) · 1.88 KB
/
ARDUINO CODE
File metadata and controls
71 lines (57 loc) · 1.88 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <Wire.h>
#include <MPU6050.h>
#include <HID-Project.h> // HID library for mouse emulation
// Initialize MPU6050 object
MPU6050 mpu;
// Variables for storing raw sensor data
int16_t ax, ay, az;
int16_t gx, gy, gz;
// Variables for cursor movement
float cursorX = 0, cursorY = 0;
float sensitivity = 0.02; // Sensitivity factor for cursor movement
int deadzone = 200; // Gyroscope deadzone to filter small unintentional movements
// Optional click threshold
int clickThreshold = 3000; // Gyroscope Z-axis value for simulating a click
bool isClicking = false;
void setup() {
// Initialize communication
Wire.begin();
Serial.begin(9600);
// Initialize MPU6050
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed. Check wiring!");
while (1);
}
Serial.println("MPU6050 initialized successfully");
// Start mouse emulation
Mouse.begin();
}
void loop() {
// Read raw sensor data
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Filter gyroscope data (apply deadzone)
float filteredGx = (abs(gx) > deadzone) ? gx : 0;
float filteredGy = (abs(gy) > deadzone) ? gy : 0;
// Map gyroscope values to cursor movement
cursorX = map(filteredGx, -32768, 32767, -10, 10) * sensitivity;
cursorY = map(filteredGy, -32768, 32767, -10, 10) * sensitivity;
// Send cursor movements
Mouse.move(cursorX, cursorY);
// Optional: Simulate left-click based on Z-axis rotation
if (gz > clickThreshold && !isClicking) {
Mouse.press(MOUSE_LEFT);
isClicking = true;
} else if (gz <= clickThreshold && isClicking) {
Mouse.release(MOUSE_LEFT);
isClicking = false;
}
// Debugging output for serial monitor
Serial.print("CursorX: ");
Serial.print(cursorX);
Serial.print(" CursorY: ");
Serial.print(cursorY);
Serial.print(" Click: ");
Serial.println(isClicking);
delay(20); // Adjust delay to control responsiveness
}