Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
engineerkid
Published © GPL3+

ARDUINO CONTROLLED GAMEPAD FOR PC (Wired)

In this project I converted an old gaming controller & made it compatible to play PC games.

BeginnerFull instructions provided5 hours4,515
ARDUINO CONTROLLED GAMEPAD FOR PC (Wired)

Things used in this project

Hardware components

Old Retro game controller
You can use a retro game controller like the one I used. If you have access to a 3d printer, you can print your own casing(This option will give you room for adding more buttons).
×1
Arduino Pro Micro
For this project you need to use an Arduino Pro Micro or Arduino Leonardo mini. In short you need a micro controller with HID (Human Interface Device) capability. Arduino Pro micro has an ATmega 32U4 micro controller.
×1
Slide Switch
This is a normal slider switch which I used for emergency purpose. The problem with working with mouse and keyboard library is that if you fail to upload a correct code you could loose the control of your keyboard or mouse. So it is very essential to have a switch which could help you to regain the control of your PC mouse/keyboard.
×1
Joystick Module
To control the mouse movement we need to use a joystick module. Each module controls the X and Y direction.
×1
Limit Switches
I used limit switches as triggers for my controller. These are optional if you want to make a game pad with just push buttons.
×4
Push Buttons
Push buttons can be interfaced in either pull up or pull down configuration. Here I used pull down configuration for all the buttons and switches. I used 10 K ohm resistors for this.
×4
UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1
General purpose PCB
Get a general purpose PCB to solder all the components.
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder wire
Spray paint (Any Color)

Story

Read more

Custom parts and enclosures

Final Project

This is how the end project will look like.

Schematics

Circuit Diagram

Follow the diagram and make all the connections.

Code

Code for Arduino Controlled Gamepad

C/C++
Before compiling the code make sure you have installed the mouse.h and keyboard.h libraries. They are necessary for this code to run.
#include<Mouse.h>
#include <Keyboard.h>
const int EMG = 2;    //Emergency stop button :) 
const int L1 = 7;
const int L2 = 8;
const int P1 = 9;
const int R1 = 14;
const int R2 = 16;
const int P2 = 10;
const int D1 = 4;
const int D2 = 5;
const int D3 = 6;
const int D4 = 3;  
const int SWITCH = 15; // digital pin 2 connected to SW output of JoyStick
const int X_AX = A1; // analog pin 0 connected to X output of JoyStick
const int Y_AX = A0; // analog pin 1 connected to Y output of JoyStick
int range = 10;               // output range or speed of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range / 4;    // resting threshold
int center = range / 2;
int EMGState = HIGH;
int L1S = LOW;
int L2S = LOW;
int P1S = LOW;
int R1S = LOW; 
int R2S = LOW; 
int P2S = LOW; 
int D1S = LOW;
int D2S = LOW;        //PREVIOUS STATES
int D3S = LOW; 
int D4S = LOW; 
void setup() 
{
  pinMode(EMG, INPUT);
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);
  pinMode(P1, INPUT);
  pinMode(D1, INPUT);
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);
  pinMode(D4, INPUT);
  pinMode(R1, INPUT);
  pinMode(R2, INPUT);
  pinMode(P2, INPUT);
  pinMode(SWITCH, INPUT_PULLUP); 
  Serial.begin(9600);
  Keyboard.begin();
  Mouse.begin();
}

void loop() 
{ 

  EMGState = digitalRead(EMG);
  if (EMGState == HIGH) 
  { 
    Serial.println("FAULT");
    Keyboard.releaseAll();
    Keyboard.end();
    Mouse.end();
  }
  else
  {
      //Serial.println("OK"); 
      int L1STATE = digitalRead(L1);
      int L2STATE = digitalRead(L2);
      int P1STATE = digitalRead(P1);
      int D1STATE = digitalRead(D1);
      int D2STATE = digitalRead(D2);
      int D3STATE = digitalRead(D3);
      int D4STATE = digitalRead(D4);
      int R1STATE = digitalRead(R1);
      int R2STATE = digitalRead(R2);
      int P2STATE = digitalRead(P2);
      
      if(L1STATE==HIGH && L1S == LOW)
      {
        Serial.println("L1 PRESSED");
        Keyboard.press('K');              //K
      }
      if(L1STATE==LOW && L1S == HIGH)
      {
        Serial.println("L1 RELEASED");
        Keyboard.release('K');              //K
      }


      if(L2STATE==HIGH && L2S == LOW)
      {
        Serial.println("L2 PRESSED");
        Keyboard.press('J');              //J
      }
      if(L2STATE==LOW && L2S == HIGH)
      {
        Serial.println("L2 RELEASED");
        Keyboard.release('J');              //J
      }


      if(P1STATE==HIGH && P1S == LOW)
      {
        Serial.println("P1 PRESSED");
        Keyboard.press('H');              //H
      }
      if(P1STATE==LOW && P1S == HIGH)
      {
        Serial.println("P1 RELEASED");
        Keyboard.release('H');              //H
      }
     
      if(R1STATE==HIGH && R1S == LOW)
      {
        Serial.println("R1 PRESSED");     //L
        Keyboard.press('L');
      }
      if(R1STATE==LOW && R1S == HIGH)
      {
        Serial.println("R1 RELEASED");     //L
        Keyboard.release('L');
      }

      if(R2STATE==HIGH && R2S == LOW)
      {
        Serial.println("R2 PRESSED");     //G
        Keyboard.press('G');
      }
      if(R2STATE==LOW && R2S == HIGH)
      {
        Serial.println("R2 RELEASED");     //G
        Keyboard.release('G');
      }

      if(P2STATE==HIGH && P2S == LOW)
      {
        Serial.println("P2 PRESSED");     //F
        Keyboard.press('F');
      }
      if(P2STATE==LOW && P2S == HIGH)
      {
        Serial.println("P2 RELEASED");     //F
        Keyboard.release('F');
      }

      
      if(D1STATE==HIGH && D1S == LOW)           
      {
        Serial.println("D1 PRESSED");     //W
        Keyboard.press('W');
      }
      if(D1STATE==LOW && D1S == HIGH)           
      {
        Serial.println("D1 RELEASED");     //W
        Keyboard.release('W');
      }

      
      if(D2STATE==HIGH && D2S==LOW)
      {
        Serial.println("D2 PRESSED");     //A
        Keyboard.press('A');
      }
      if(D2STATE==LOW && D2S==HIGH)
      {
        Serial.println("D2 RELEASED");     //A
        Keyboard.release('A');
      }

      
      if(D3STATE==HIGH && D3S==LOW)
      {
        Serial.println("D3 PRESSED");     //S
        Keyboard.press('S');
      }
      if(D3STATE==LOW && D3S==HIGH)
      {
        Serial.println("D3 RELEASED");     //S
        Keyboard.release('S');
      }


      if(D4STATE==HIGH && D4S==LOW)
      {
        Serial.println("D4 PRESSED");     //D
        Keyboard.press('D');
      }
      if(D4STATE==LOW && D4S==HIGH)
      {
        Serial.println("D4 RELEASED");     //D
        Keyboard.release('D');
      }
      
      L1S = L1STATE;
      L2S = L2STATE;
      P1S = P1STATE;
      R1S = R1STATE;
      R2S = R2STATE;
      P2S = P2STATE;
      D1S = D1STATE;
      D2S = D2STATE;
      D3S = D3STATE;
      D4S = D4STATE;
     

     int xReading = readAxis(A1);
     int yReading = readAxis(A0);
     Mouse.move(xReading, yReading, 0);
     if (digitalRead(SWITCH) == LOW) 
       {
         if (!Mouse.isPressed(MOUSE_LEFT)) 
         {
           Mouse.press(MOUSE_LEFT);
         }
       }
      else 
      {
      if (Mouse.isPressed(MOUSE_LEFT)) 
      {
        Mouse.release(MOUSE_LEFT);
      }
      }

        delay(responseDelay);
 }
} 
int readAxis(int thisAxis) 
{
  int reading = analogRead(thisAxis);
  reading = map(reading, 0, 1023, 0, range);
  int distance = reading - center;
  if (abs(distance) < threshold) 
  {
    distance = 0;
  }

  return distance;
}

Credits

engineerkid
13 projects • 0 followers
How To Protect Yourself From Coronavirus click above link☝☝☝
Thanks to Arduino.

Comments