Who wan't a Game controller!

I used arduino leonardo as arduino uno can't be recognized in the computer as keyboard or mouse i use 2 analog joystick code will be provided below:

#include <Keyboard.h>  // Use built-in Keyboard library
#include <HID_Buttons.h>  // Must import AFTER Keyboard.h
#include <Mouse.h> // For controller2 which is gonna be for mouse

const uint8_t Pin_XAxis = A0;  // Pin for joy horizontal axis
const uint8_t Pin_YAxis = A1;  // Pin for joy vertical axis

const int JoyCenter   = 1023 / 2;  // max value for 10-bit ADC / 2
const int JoyDeadzone = 50;        // +/- area around the center to ignore

int horzPin = A2;  // Analog output of horizontal joystick pin
int vertPin = A3;  // Analog output of vertical joystick pin
int selPin = 9;  // select button pin of joystick

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 = 215;  // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;

//int invertMouse = 1;        //Invert joystick based on orientation
int invertMouse = -1;         //Noninverted joystick based on orientation


//Error Occurs after some time of use please change if joystick doesn't work!
KeyboardButton moveForward('A');
KeyboardButton moveLeft('S');
KeyboardButton moveBackward('D');
KeyboardButton moveRight('W');

void setup() {
  Keyboard.begin();
  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
  delay(1000);  // 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() {
  int x = analogRead(Pin_XAxis);  // Read X axis
  int y = analogRead(Pin_YAxis);  // Read Y axis
  
  moveLeft.set(x < JoyCenter - JoyDeadzone);
	moveRight.set(x > JoyCenter + JoyDeadzone);

	moveForward.set(y < JoyCenter - JoyDeadzone);
	moveBackward.set(y > JoyCenter + JoyDeadzone);
	
  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset

  if (vertValue != 0)
  {
    Mouse.move(0, (invertMouse * (horzValue / sensitivity)), 0); // move mouse on y axis
  }
  if (horzValue != 0)
  {
    Mouse.move((invertMouse * (vertValue / sensitivity)), 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
    Serial.println("Joystick button is clicked");
  }
  else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the joystick button is not pressed
  {
    mouseClickFlag = 0;
    Mouse.release(MOUSE_LEFT);  // release the left button
  }
}

Your welcome if i helped you create a game controller but: A1 to 1st joystick Pin x, A2 to 1st joystick y, A3 to 2st joystick x, A4 to 2st joystick y

int horzPin = A2;  // Analog output of horizontal joystick pin
int vertPin = A3;  // Analog output of vertical joystick pin
int selPin = 9;  // select button pin of joystick

sp.

const byte horzPin = A2;  // Analog output of horizontal joystick pin
const byte vertPin = A3;  // Analog output of vertical joystick pin
const byte selPin = 9;  // select button pin of joystick

:rage: :rage: :triumph:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.