Hi all,
first thing first: thank you for reading this.
I am trying to write a sketch in order to control mouse position on the screen while moving an accelerometer/ giroscope module. The module is a common MPU6050.
The target application is a sort of "lightgun" (or should i call it an accelGun or gyroGun) for MAME/arcade games: if i move/tilt the "gun", the mouse moves accordingly to a specific position.
I found this great MouseTo library which could be used for absolute mouse movements. I wrote down a simple sketch (with the help of other libraries and sheets of code found here and there) and this is my result up to now:
#include <MouseTo.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>
#define ZERO 6
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz, axZero, ayZero, azZero;
void setup() {
pinMode(ZERO, INPUT_PULLUP);
digitalWrite(ZERO, HIGH);
Wire.begin();
Mouse.begin();
MouseTo.setCorrectionFactor(1);
MouseTo.setScreenResolution(1366, 768);
mpu.initialize();
if (!mpu.testConnection()) {
while (1);
}
}
void loop() {
int zeroState = digitalRead(ZERO);
int xRes = MouseTo.getScreenResolutionX();
int yRes = MouseTo.getScreenResolutionY();
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
int vx = map(ax-axZero, -16000, 16000, xRes, 0);
int vy = map(ay-ayZero, -16000, 16000, yRes, 0);
if (zeroState== HIGH) {
MouseTo.setTarget(vx, vy);
while (MouseTo.move() == false) {}
}
if (zeroState == LOW){
axZero = ax;
ayZero = ay;
}
delay(20);
}
This sketch kinda of works, but the mouse pointer follows "square" trajectories (starting from the 0,0 coordinate of the screen (upper left corner) to the final position instead of remaining in the latest position get (homing in the latest position) ...
any help would be much appreciated!