Components and supplies
Pushbutton Switch, Pushbutton
Arduino UNO
Jumper wires (generic)
6 DOF Sensor - MPU6050
Apps and platforms
Arduino IDE
python
Project description
Code
port1.py
python
before trying this install pyautogui by the command "pip install pyautogui", try this first if doesn't then try port2.py
1# this is port1.py, first python program if this doesn't work on your game then use port2.py 2 3import time 4import pyautogui as pg 5import serial 6 7print("i wil give input after 5s so") 8for i in range(5): 9 print(i, end = "\ 10") 11 time.sleep(1) 12ser = serial.Serial('COM3', baudrate=9600, timeout=1) # change COM3 to your port in which arduino is connected 13while True: 14 portdata = ser.readline().decode('ascii') # read the data comming from arduino 15 if len(portdata) == 0: 16 print("no data comming........", end = "\ 17") # port is connected but no data is avaliable on serial port 18 ser.flushInput() 19 if len(portdata) != 0: # if data is avaliable on the serial port 20 if portdata[0] == 'q': # check the data 21 pg.keyDown('w') #simulate key input 22 pg.keyDown('a') 23 time.sleep(0.2) 24 pg.keyUp('w') 25 pg.keyUp('a') 26 ser.flushInput() # clear the queues of data on Serial port that stored while simulating key input 27 elif portdata[0] == 'e': 28 pg.keyDown('w') 29 pg.keyDown('d') 30 time.sleep(0.2) 31 pg.keyUp('w') 32 pg.keyUp('d') 33 ser.flushInput() 34 elif portdata[0] == 'z': 35 pg.keyDown('s') 36 pg.keyDown('a') 37 time.sleep(0.2) 38 pg.keyUp('s') 39 pg.keyUp('a') 40 ser.flushInput() 41 elif portdata[0] == 'c': 42 pg.keyDown('s') 43 pg.keyDown('d') 44 time.sleep(0.2) 45 pg.keyUp('s') 46 pg.keyUp('d') 47 ser.flushInput() 48 elif portdata[0] == 'w': 49 pg.keyDown('w') 50 time.sleep(0.1) 51 pg.keyUp('w') 52 ser.flushInput() 53 elif portdata[0] == 's': 54 pg.keyDown('s') 55 time.sleep(0.1) 56 pg.keyUp('s') 57 ser.flushInput() 58 elif portdata[0] == 'a': 59 pg.keyDown('a') 60 time.sleep(0.1) 61 pg.keyUp('a') 62 ser.flushInput() 63 elif portdata[0] == 'd': 64 pg.keyDown('d') 65 time.sleep(0.2) 66 pg.keyUp('d') 67 ser.flushInput() 68
Arduino_game_code.ino
arduino
Upload this code to your arduino uno
1#include <MPU6050_tockn.h> 2#include <Wire.h> 3 4MPU6050 mpu6050(Wire); 5 6void setup() { 7 Serial.begin(9600); //start the serial communication 8 Wire.begin(); // start I2C communication for comunicating with MPU6050 9 mpu6050.begin(); 10 mpu6050.calcGyroOffsets(true); 11 pinMode(A1,INPUT); // throttle button signal pin 12 pinMode(A0,INPUT); // brake button signal pin 13} 14 15void loop() { 16 mpu6050.update(); 17 if(mpu6050.getAngleZ()>25 && digitalRead(A0)) // check if forward(throttle) and left 18 Serial.println('q'); // print 'q' on the serial port 19 else if(mpu6050.getAngleZ()<-25 && digitalRead(A0)) // check if forward(throttle) and right 20 Serial.println('e'); // print 'e' on the Serial Port 21 else if(mpu6050.getAngleZ()>25 && digitalRead(A1)) // check if backward(brake) and left 22 Serial.println('z'); // print 'z' on the Serial Port 23 else if(mpu6050.getAngleZ()<-25 && digitalRead(A1)) // check if backward(brake) and right 24 Serial.println('c'); // print 'c' on the Serial Port 25 else if(digitalRead(A0)) // check if forward(throttle) 26 Serial.println('w'); // print 'w' on the serial port 27 else if(digitalRead(A1)) // check if backward(brake) 28 Serial.println('s'); // print 's' on the Serial 29 else if(mpu6050.getAngleZ()>25) // check if left 30 Serial.println('a'); // print 'a' on the Serial Port 31 else if(mpu6050.getAngleZ()<-25) // check if right 32 Serial.println('d'); // print 'd' on the Serial Port 33}
port2.py
python
this is port2.py, second python program use this if first one doesn't work, and this also need a additional program file so also place that on the same folder am also giving that additional program
1# check this program if work then Enjoy , this will probably work on all high end games(Direct X games) 2 3import serial 4import time 5from directkey import PressKey, ReleaseKey, W, A, S, D 6 7ser = serial.Serial('COM3', baudrate=9600, timeout=1) # change COM3 to your port in which arduino is connected 8while True: 9 portdata = ser.readline().decode('ascii') # read the data comming from arduino 10 if len(portdata) == 0: 11 print("no data comming........", end = "\ 12") # port is connected but no data is avaliable on serial port 13 ser.flushInput() 14 if len(portdata) != 0: # if data is avaliable on the serial port 15 if portdata[0] == 'q': # check the data 16 PressKey(W) #simulate key input 17 PressKey(A) 18 time.sleep(0.2) 19 ReleaseKey(W) 20 ReleaseKey(A) 21 ser.flushInput() # clear the queues of data on Serial port that stored while simulating key input 22 elif portdata[0] == 'e': 23 PressKey(W) 24 PressKey(D) 25 time.sleep(0.2) 26 ReleaseKey(W) 27 ReleaseKey(D) 28 ser.flushInput() 29 elif portdata[0] == 'z': 30 PressKey(S) 31 PressKey(A) 32 time.sleep(0.2) 33 ReleaseKey(S) 34 ReleaseKey(A) 35 ser.flushInput() 36 elif portdata[0] == 'c': 37 PressKey(S) 38 PressKey(D) 39 time.sleep(0.2) 40 ReleaseKey(S) 41 ReleaseKey(D) 42 ser.flushInput() 43 elif portdata[0] == 'w': 44 PressKey(W) 45 time.sleep(0.2) 46 ReleaseKey(W) 47 ser.flushInput() 48 elif portdata[0] == 's': 49 PressKey(S) 50 time.sleep(0.2) 51 ReleaseKey(S) 52 ser.flushInput() 53 elif portdata[0] == 'a': 54 PressKey(A) 55 time.sleep(0.2) 56 ReleaseKey(A) 57 ser.flushInput() 58 elif portdata[0] == 'd': 59 PressKey(D) 60 time.sleep(0.2) 61 ReleaseKey(D) 62 ser.flushInput() 63
port2.py
python
this is port2.py, second python program use this if first one doesn't work, and this also need a additional program file so also place that on the same folder am also giving that additional program
1# check this program if work then Enjoy , this will probably work on all high end games(Direct X games) 2 3import serial 4import time 5from directkey import PressKey, ReleaseKey, W, A, S, D 6 7ser = serial.Serial('COM3', baudrate=9600, timeout=1) # change COM3 to your port in which arduino is connected 8while True: 9 portdata = ser.readline().decode('ascii') # read the data comming from arduino 10 if len(portdata) == 0: 11 print("no data comming........", end = "\ 12") # port is connected but no data is avaliable on serial port 13 ser.flushInput() 14 if len(portdata) != 0: # if data is avaliable on the serial port 15 if portdata[0] == 'q': # check the data 16 PressKey(W) #simulate key input 17 PressKey(A) 18 time.sleep(0.2) 19 ReleaseKey(W) 20 ReleaseKey(A) 21 ser.flushInput() # clear the queues of data on Serial port that stored while simulating key input 22 elif portdata[0] == 'e': 23 PressKey(W) 24 PressKey(D) 25 time.sleep(0.2) 26 ReleaseKey(W) 27 ReleaseKey(D) 28 ser.flushInput() 29 elif portdata[0] == 'z': 30 PressKey(S) 31 PressKey(A) 32 time.sleep(0.2) 33 ReleaseKey(S) 34 ReleaseKey(A) 35 ser.flushInput() 36 elif portdata[0] == 'c': 37 PressKey(S) 38 PressKey(D) 39 time.sleep(0.2) 40 ReleaseKey(S) 41 ReleaseKey(D) 42 ser.flushInput() 43 elif portdata[0] == 'w': 44 PressKey(W) 45 time.sleep(0.2) 46 ReleaseKey(W) 47 ser.flushInput() 48 elif portdata[0] == 's': 49 PressKey(S) 50 time.sleep(0.2) 51 ReleaseKey(S) 52 ser.flushInput() 53 elif portdata[0] == 'a': 54 PressKey(A) 55 time.sleep(0.2) 56 ReleaseKey(A) 57 ser.flushInput() 58 elif portdata[0] == 'd': 59 PressKey(D) 60 time.sleep(0.2) 61 ReleaseKey(D) 62 ser.flushInput() 63
directkey.py
python
this is additional program required for port2.py so place them in same folder while running port2.py but don't run this, Only run port2.py that will automatically import the requirement from this program file
1import ctypes 2import time 3 4SendInput = ctypes.windll.user32.SendInput 5 6 7W = 0x11 8A = 0x1E 9S = 0x1F 10D = 0x20 11 12NP_2 = 0x50 13NP_4 = 0x4B 14NP_6 = 0x4D 15NP_8 = 0x48 16 17# C struct redefinitions 18PUL = ctypes.POINTER(ctypes.c_ulong) 19class KeyBdInput(ctypes.Structure): 20 _fields_ = [("wVk", ctypes.c_ushort), 21 ("wScan", ctypes.c_ushort), 22 ("dwFlags", ctypes.c_ulong), 23 ("time", ctypes.c_ulong), 24 ("dwExtraInfo", PUL)] 25 26class HardwareInput(ctypes.Structure): 27 _fields_ = [("uMsg", ctypes.c_ulong), 28 ("wParamL", ctypes.c_short), 29 ("wParamH", ctypes.c_ushort)] 30 31class MouseInput(ctypes.Structure): 32 _fields_ = [("dx", ctypes.c_long), 33 ("dy", ctypes.c_long), 34 ("mouseData", ctypes.c_ulong), 35 ("dwFlags", ctypes.c_ulong), 36 ("time",ctypes.c_ulong), 37 ("dwExtraInfo", PUL)] 38 39class Input_I(ctypes.Union): 40 _fields_ = [("ki", KeyBdInput), 41 ("mi", MouseInput), 42 ("hi", HardwareInput)] 43 44class Input(ctypes.Structure): 45 _fields_ = [("type", ctypes.c_ulong), 46 ("ii", Input_I)] 47 48# Actuals Functions 49 50def PressKey(hexKeyCode): 51 extra = ctypes.c_ulong(0) 52 ii_ = Input_I() 53 ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) ) 54 x = Input( ctypes.c_ulong(1), ii_ ) 55 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) 56 57def ReleaseKey(hexKeyCode): 58 extra = ctypes.c_ulong(0) 59 ii_ = Input_I() 60 ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) ) 61 x = Input( ctypes.c_ulong(1), ii_ ) 62 ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) 63 64if __name__ == '__main__': 65 PressKey(0x11) 66 time.sleep(1) 67 ReleaseKey(0x11) 68 time.sleep(1)
port1.py
python
before trying this install pyautogui by the command "pip install pyautogui", try this first if doesn't then try port2.py
1# this is port1.py, first python program if this doesn't work on your 2 game then use port2.py 3 4import time 5import pyautogui as pg 6import serial 7 8print("i 9 wil give input after 5s so") 10for i in range(5): 11 print(i, end = "\ 12") 13 14 time.sleep(1) 15ser = serial.Serial('COM3', baudrate=9600, timeout=1) # change 16 COM3 to your port in which arduino is connected 17while True: 18 portdata = 19 ser.readline().decode('ascii') # read the data comming from arduino 20 21 if len(portdata) == 0: 22 print("no data comming........", end = "\ 23") 24 # port is connected but no data is avaliable on serial port 25 ser.flushInput() 26 27 if len(portdata) != 0: # if data is avaliable on 28 the serial port 29 if portdata[0] == 'q': # check 30 the data 31 pg.keyDown('w') #simulate key 32 input 33 pg.keyDown('a') 34 time.sleep(0.2) 35 pg.keyUp('w') 36 37 pg.keyUp('a') 38 ser.flushInput() # 39 clear the queues of data on Serial port that stored while simulating key input 40 41 elif portdata[0] == 'e': 42 pg.keyDown('w') 43 pg.keyDown('d') 44 45 time.sleep(0.2) 46 pg.keyUp('w') 47 pg.keyUp('d') 48 49 ser.flushInput() 50 elif portdata[0] == 'z': 51 pg.keyDown('s') 52 53 pg.keyDown('a') 54 time.sleep(0.2) 55 pg.keyUp('s') 56 57 pg.keyUp('a') 58 ser.flushInput() 59 elif portdata[0] 60 == 'c': 61 pg.keyDown('s') 62 pg.keyDown('d') 63 time.sleep(0.2) 64 65 pg.keyUp('s') 66 pg.keyUp('d') 67 ser.flushInput() 68 69 elif portdata[0] == 'w': 70 pg.keyDown('w') 71 time.sleep(0.1) 72 73 pg.keyUp('w') 74 ser.flushInput() 75 elif portdata[0] 76 == 's': 77 pg.keyDown('s') 78 time.sleep(0.1) 79 pg.keyUp('s') 80 81 ser.flushInput() 82 elif portdata[0] == 'a': 83 pg.keyDown('a') 84 85 time.sleep(0.1) 86 pg.keyUp('a') 87 ser.flushInput() 88 89 elif portdata[0] == 'd': 90 pg.keyDown('d') 91 time.sleep(0.2) 92 93 pg.keyUp('d') 94 ser.flushInput() 95
Arduino_game_code.ino
arduino
Upload this code to your arduino uno
1#include <MPU6050_tockn.h> 2#include <Wire.h> 3 4MPU6050 mpu6050(Wire); 5 6void 7 setup() { 8 Serial.begin(9600); //start the serial communication 9 Wire.begin(); 10 // start I2C communication for comunicating with MPU6050 11 mpu6050.begin(); 12 13 mpu6050.calcGyroOffsets(true); 14 pinMode(A1,INPUT); // throttle 15 button signal pin 16 pinMode(A0,INPUT); // brake button signal pin 17} 18 19void 20 loop() { 21 mpu6050.update(); 22 if(mpu6050.getAngleZ()>25 && digitalRead(A0)) 23 // check if forward(throttle) and left 24 Serial.println('q'); // 25 print 'q' on the serial port 26 else if(mpu6050.getAngleZ()<-25 && digitalRead(A0)) 27 // check if forward(throttle) and right 28 Serial.println('e'); // 29 print 'e' on the Serial Port 30 else if(mpu6050.getAngleZ()>25 && digitalRead(A1)) 31 // check if backward(brake) and left 32 Serial.println('z'); // 33 print 'z' on the Serial Port 34 else if(mpu6050.getAngleZ()<-25 && digitalRead(A1)) 35 // check if backward(brake) and right 36 Serial.println('c'); // 37 print 'c' on the Serial Port 38 else if(digitalRead(A0)) // 39 check if forward(throttle) 40 Serial.println('w'); // 41 print 'w' on the serial port 42 else if(digitalRead(A1)) // 43 check if backward(brake) 44 Serial.println('s'); // 45 print 's' on the Serial 46 else if(mpu6050.getAngleZ()>25) // 47 check if left 48 Serial.println('a'); // print 49 'a' on the Serial Port 50 else if(mpu6050.getAngleZ()<-25) // 51 check if right 52 Serial.println('d'); // print 53 'd' on the Serial Port 54}
Downloadable files
Circuit Diagram
diagram is very easy and simple so connect wire, upload the program and enjoy
Circuit Diagram
Circuit Diagram
diagram is very easy and simple so connect wire, upload the program and enjoy
Circuit Diagram
Comments
Only logged in users can leave comments