#include <Mouse.h>
float pulsesX, moveX,mouseX, lastPulsesX, XA_SIG=0, XB_SIG=1, pulsesY, moveY,mouseY, lastPulsesY, YA_SIG=0, YB_SIG=1;
int wheelsSpeed;
float panSpeed[4];
float tiltSpeed[4];
boolean buttonState, lastButtonState;
void setup(){
attachInterrupt(1, XA_RISE, RISING); // Pin 2
attachInterrupt(0, XB_RISE, RISING); // Pin 3
attachInterrupt(2, YA_RISE, RISING); // Pin 0
attachInterrupt(3, YB_RISE, RISING); // Pin 1
pinMode (8, INPUT_PULLUP); // pushButton
Mouse.begin();
Serial.begin(115200);
delay(100);
Serial.println("Connected");
// Defining speed coeff to match Arrihead II
panSpeed[1] = 9.71;
panSpeed[2] = 5.1;
panSpeed[3] = 2.84;
tiltSpeed[1] = 12.45;
tiltSpeed[2] = 6.58;
tiltSpeed[3] = 3.38;
wheelsSpeed = 1; // Default speed when rebooting the arduino
}
void loop(){
buttonState=digitalRead(8); // Speed selection pushbutton
if ( buttonState != lastButtonState && buttonState == 0) {
wheelsSpeed = wheelsSpeed +1;
if (wheelsSpeed >= 4) {
wheelsSpeed = 1;
}
}
// Calculating wheels movement
moveX = lastPulsesX - pulsesX;
moveY = pulsesY - lastPulsesY;
// Mapping to the Arrihead speed
mouseX = moveX / panSpeed[wheelsSpeed];
mouseY = moveY / tiltSpeed[wheelsSpeed];
Mouse.move (mouseX, mouseY);
Serial.print("x");
Serial.print(wheelsSpeed);
Serial.print("y");
Serial.print(pulsesY);
Serial.println("end");
lastPulsesX = pulsesX; // Storing last value for X movement
lastPulsesY = pulsesY; // Storing last value for Y movement
lastButtonState = buttonState;
delay(20);
}
//X-Axis
void XA_RISE(){
detachInterrupt(1);
//delay(1);
XA_SIG=1;
if(XB_SIG==0)
pulsesX++;//moving forward
if(XB_SIG==1)
pulsesX--;//moving reverse
attachInterrupt(1, XA_FALL, FALLING);
}
void XA_FALL(){
detachInterrupt(1);
//delay(1);
XA_SIG=0;
if(XB_SIG==1)
pulsesX++;//moving forward
if(XB_SIG==0)
pulsesX--;//moving reverse
attachInterrupt(1, XA_RISE, RISING);
}
void XB_RISE(){
detachInterrupt(0);
//delay(1);
XB_SIG=1;
if(XA_SIG==1)
pulsesX++;//moving forward
if(XA_SIG==0)
pulsesX--;//moving reverse
attachInterrupt(0, XB_FALL, FALLING);
}
void XB_FALL(){
detachInterrupt(0);
//delay(1);
XB_SIG=0;
if(XA_SIG==0)
pulsesX++;//moving forward
if(XA_SIG==1)
pulsesX--;//moving reverse
attachInterrupt(0, XB_RISE, RISING);
}
//Y-Axis
void YA_RISE(){
detachInterrupt(2);
//delay(1);
YA_SIG=1;
if(YB_SIG==0)
pulsesY++;//moving forward
if(YB_SIG==1)
pulsesY--;//moving reverse
attachInterrupt(2, YA_FALL, FALLING);
}
void YA_FALL(){
detachInterrupt(2);
//delay(1);
YA_SIG=0;
if(YB_SIG==1)
pulsesY++;//moving forward
if(YB_SIG==0)
pulsesY--;//moving reverse
attachInterrupt(2, YA_RISE, RISING);
}
void YB_RISE(){
detachInterrupt(3);
//delay(1);
YB_SIG=1;
if(YA_SIG==1)
pulsesY++;//moving forward
if(YA_SIG==0)
pulsesY--;//moving reverse
attachInterrupt(3, YB_FALL, FALLING);
}
void YB_FALL(){
detachInterrupt(3);
//delay(1);
YB_SIG=0;
if(YA_SIG==0)
pulsesY++;//moving forward
if(YA_SIG==1)
pulsesY--;//moving reverse
attachInterrupt(3, YB_RISE, RISING);
}