jasrajdevs
Published

Arduino UNO game controller

A controller made with c++ and python

IntermediateWork in progress30 minutes710
Arduino UNO game controller

Things used in this project

Story

Read more

Schematics

circuit

connect according to it

Code

Arduino code

C/C++
upload it
#define x2 A2
#define x A0
#define y A1

#define VCC2 9
#define j 8
#define VCC3 10
#define GND2 6
#define s 7
int X;
int Y;
int X2;
int J;
int S;


void setup(){
  Serial.begin(9600);
  pinMode(VCC2, OUTPUT);
  digitalWrite(VCC2, HIGH);
  pinMode(VCC3, OUTPUT);
  digitalWrite(VCC3, HIGH);
  pinMode(GND2, OUTPUT);
  digitalWrite(GND2, LOW);
}
void loop(){
  X = analogRead(x);
  Y = analogRead(y);
  X2 = analogRead(x2);
  J = digitalRead(j);
  S = digitalRead(s);
  Serial.print(X);
  Serial.print(",");
  Serial.print(Y); 
  Serial.print(",");
  Serial.print(X2);
  Serial.print(",");
  Serial.print(J);
  Serial.print(",");
  Serial.print(S);
  Serial.print(",");
  Serial.println("0");

  delay(100);
}

Python code

Python
Run it and you will see "port.port" there you write com in which you have connected arduino
import serial.tools.list_ports
import pydirectinput
import mouse
import ctypes
import win32api, win32con
port = serial.Serial()
s = 60
port.baudrate = 9600
port.port = "COM3"
port.open()
keysDown = {}

def kD(key):              
    keysDown[key] = True        
    pydirectinput.keyDown(key)
    
def kU(key):                     
    if key in keysDown:
        del (keysDown[key])         
        pydirectinput.keyUp(key)

def move(D):
    if (D=='left'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -s, 0)
    elif (D=='right'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, s, 0)
    elif(D=='up'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, -s)
    elif(D=='down'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, s)


def main():
    while True:
        if port.in_waiting:
            packet=port.readline()
            
            data = packet.decode('utf')
            value = data.split(',')
            final = list(value)
            ux = final[0]
            uy = final[1]
            uj = final[3]
            us = final[4]
            uxx = final[2]
            x = int(ux)
            y = int(uy)
            xx = int(uxx)
            j = int(uj)
            s = int(us)
        
            if(y>900):
                kD('left')
                kU('right')
            elif(y<20):
                kD('right')
                kU('left')
            else:
                kU('left')
                kU('right')
            if (x>900):
                move('down')
            elif(x<80):
                move('up')
            if (xx>=650):
                kD('down')
                kU('up')
            elif(xx<100):
                kD('up')
                kU('down')
            else:
                kU('down')
                kU('up')
            if(s==0):
                kD('space')

            elif(j==0):
                kD('shift')

            else:

                kU('space')
                kU('shift')
                move('no')


main()

Credits

jasrajdevs

jasrajdevs

7 projects • 10 followers
Developing since 2020 july..... I am a student.

Comments