//Define inttypes to the 6 axis values which will be obtained from the MPU6050 int16_t Ax, Ay, Az, Gx, Gy, Gz; // From examples from the library MPU6050 mpu;
//Define the values that will be sent to the Mouse int mx; int my; //Define a scaler value to change the sensitivity of the mouse int scaler = 100;
//Smoothing of mouse movement const int nReadings = 5; int xReadings[nReadings]; int yReadings[nReadings]; int xTot; int yTot; int xAvg; int yAvg; int readIndex;
//Timing control //----> Mouse movement unsigned long prevMouseMillis = 0; const long mouseInterval = 5; //10 is a safe value
//Pressure click and scroll #define pPin A0 int p = 0; int pTimerPos = 0; int pTimerNeg = 0; int pThresh = 50; int clickTrigger = 300; int scrollTrigger = 800; int scrollDist = 10;
// Define pins for debuging mouse clicks, this is just LEDs #define leftLedPin 4 #define rightLedPin 5
// On startup callebrate ambient pressure int cPressure = 0; int aPressure = 500; //will update this value with cPressure
void setup() {
Serial.begin(9600); //mostly for debugging pinMode(leftLedPin, OUTPUT); pinMode(rightLedPin, OUTPUT);
pinMode(pPin, INPUT);
// initialize the mpu mpu.initialize(); if (!mpu.testConnection()) { while (1); } else { Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); }
// Smoothing of mouse movement, initialize empty arrays for (int i = 0; i < nReadings; i++) { xReadings[i] = 0; yReadings[i] = 0; }
//callibrate to ambient pressure
for (int k = 0; k < 10; k++) { cPressure = cPressure + analogRead(pPin); } aPressure = cPressure/10;
//end of setup
}
void loop() {
// Mouse movement
unsigned long currentMouseMillis = millis();
if (currentMouseMillis - prevMouseMillis >= mouseInterval) { prevMouseMillis = currentMouseMillis;
mpu.getMotion6(&Ax, &Ay, &Az, &Gx, &Gy, &Gz); mx = -(Gz - 200) / scaler; //adjust offsets for the gyro to cancel drift my = (Gy - 225) / scaler;
//smoothing of mouse movement xTot = xTot - xReadings[readIndex]; yTot = yTot - yReadings[readIndex];