#include <Mouse.h>
int horzPin = A1; // Analog output of horizontal joystick pin
int vertPin = A0; // Analog output of vertical joystick pin
int selPin = 8; // select button pin of joystick
int selPin1 = 9; //sağ
int selPin2 = 7; //orta
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
int vertValue, horzValue; // Stores current analog output of each axis
const int sensitivity = 100 ; // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;
int mouseClickFlag1 = 0;
int mouseClickFlag2 = 0;
//int invertMouse = 1; //Invert joystick based on orientation
int invertMouse = -1; //Noninverted joystick based on orientation
void setup()
{
Serial.begin(9600);
pinMode(horzPin, INPUT); // Set both analog pins as inputs
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT); // set button select pin as input
digitalWrite(selPin, HIGH); // Pull button select pin high
digitalWrite(selPin1, HIGH); // Pull button select pin high
digitalWrite(selPin2, HIGH); // Pull button select pin high
delay(500); // short delay to let outputs settle
vertZero = analogRead(vertPin); // get the initial values
horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these
Mouse.begin(); //Init mouse emulation
}
void loop()
{
vertValue = analogRead(vertPin) - vertZero; // read vertical offset
horzValue = analogRead(horzPin) - horzZero; // read horizontal offset
if (vertValue != 0)
Mouse.move(0, (invertMouse * (vertValue / sensitivity)) * -1, 0); // move mouse on y axis
if (horzValue != 0)
Mouse.move((invertMouse * (horzValue / sensitivity)) * 1, 0, 0); // move mouse on x axis
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // if the joystick button is pressed
{
mouseClickFlag = 1;
Mouse.press(MOUSE_LEFT); // click the left button down
}
else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the joystick button is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_LEFT); // release the left button
}
if ((digitalRead(selPin1) == 0) && (!mouseClickFlag1)) // if the joystick button is pressed
{
mouseClickFlag1 = 1;
Mouse.press(MOUSE_RIGHT); // click the left button down
}
else if ((digitalRead(selPin1)) && (mouseClickFlag1)) // if the joystick button is not pressed
{
mouseClickFlag1 = 0;
Mouse.release(MOUSE_RIGHT); // release the left button
}
if ((digitalRead(selPin2) == 0) && (!mouseClickFlag2)) // if the joystick button is pressed
{
mouseClickFlag2 = 1;
Mouse.press(MOUSE_MIDDLE); // click the left button down
}
else if ((digitalRead(selPin2)) && (mouseClickFlag2)) // if the joystick button is not pressed
{
mouseClickFlag2 = 0;
Mouse.release(MOUSE_MIDDLE); // release the left button
}
}