43foldz
Published © GPL3+

Arduino game controler

Custom Arduino game controler with six buttons.

IntermediateFull instructions provided175
Arduino game controler

Things used in this project

Hardware components

Analog joystick (Generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Arduino Leonardo
Arduino Leonardo
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

screen_shot_2021-09-02_at_4_18_56_pm_ELt8fvXADH.png

joystick connections
sw=9
y=A1
x=A0
5v
gnd

Code

code for game controler

C/C++
you cannot use an Arduino uno for this project you myst use a board that has a 32u4 chip Leonardo and pro micro are the only ones that i know.
#include <Keyboard.h>
#include <Mouse.h>
int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A1;  // 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 = 150;  // 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
 
// Emulating keyboard or mouse functions only work on 32u4 or SAMD micro-based boards.
void setup() {
  
  //Configure pins as an input and enable the internal pull-up resistor,
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  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
  Keyboard.begin();
  Mouse.begin(); //Init mouse emulation
}
void loop() {
//If corresponding button is pressed
  if (digitalRead(2) == LOW) {
    //Send an ASCII 'W',
    Keyboard.write(87);
    
    
  }
 if (digitalRead(3) == LOW) {
    //Send an ASCII 'A'
    Keyboard.write(65);
    
  }

 if (digitalRead(4) == LOW) {
    //Send an ASCII 'S',
    Keyboard.write(83);
    
  }

 if (digitalRead(5) == LOW) {
    //Send an ASCII 'D',
    Keyboard.write(68);
    
  }
  
  if (digitalRead(6) == LOW) {
    //Send an ASCII 'shift',
    Keyboard.write(16);
    
  }
  if (digitalRead(7) == LOW) {
    //Send an ASCII 'space',
    Keyboard.write(32);
    
  }
 else {
    Keyboard.releaseAll();
}
vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset

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

Credits

43foldz

43foldz

1 project • 0 followers

Comments