I have found some code online and am wondering which part of it is for the click and drag functionality. I would like to take this part of the code and transfer it into a different sketch. Can anyone help?
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;
int button1 = 6;
int button2 = 7;
int buttonState1 = 0;
int buttonState2 = 0;
int angleToDistance(int a)
{
if (a < -80)
{
return -40;
}
else if (a < -65) {
return -20;
}
else if (a < -50) {
return -10;
}
else if (a < -15) {
return -5;
}
else if (a < -5) {
return -1;
}
else if (a > 80) {
return 40;
}
else if (a > 65) {
return 20;
}
else if (a > 15) {
return 10;
}
else if (a > 5) {
return 1;
}
}
void setup() {
Serial.begin(9600);
Mouse.begin();
pinMode(vx, INPUT);
pinMode(vy, INPUT);
pinMode(6, INPUT_PULLUP);
pinMode(6, INPUT);
digitalWrite(6, HIGH);
pinMode(7, INPUT);
digitalWrite(7, HIGH);
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
while (1);
}
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
int vx = map(ax, -16000, 16000, 90, -90);
int vy = map(ay, -16000, 16000, 90, -90);
attachInterrupt(1, keyboard, CHANGE);
attachInterrupt(7,fire,HIGH);
Mouse.move(angleToDistance(vx), angleToDistance(vy));
if (digitalRead(6) == LOW) {
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else {
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
if (digitalRead(7) == LOW) {
if (!Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.press(MOUSE_RIGHT);
}
}
else {
if (Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.release(MOUSE_RIGHT);
}
}
delay(20);
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(6);
Serial.print("X: ");
Serial.print(vx);
Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);
}
void keyboard()
{
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(6);
Serial.print("X: ");
Serial.print(vx);
Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);
}
void fire()
{
}