Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
noah_arduino
Published

Game Controller with Arduino Uno and Python

how to make a game controller with arduino uno and python

IntermediateFull instructions provided4,568
Game Controller with Arduino Uno and Python

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Sparkfun push button
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×5

Software apps and online services

Arduino IDE
Arduino IDE
Python

Story

Read more

Code

Python Code

Python
import serial
from pynput.keyboard import Controller

print("Hello Welcome to NBox 100")

print("""Please enter the COM port of your controller.
You can find it if you go to your device manager and look for COM & LPT
section. find your device by unpluging it and pluging it again.
You write it like:
ex: COM8
""")

ser = serial.Serial(input(">>"), 9600)

print("connection established")

keyboard = Controller()

while True:
    data = ser.readline()
    
    if data.decode().strip() == "d":
        keyboard.press("d")

    if data.decode().strip() == "!d":
        keyboard.release("d")

    if data.decode().strip() == "a":
        keyboard.press("a")

    if data.decode().strip() == "!a":
        keyboard.release("a")

    if data.decode().strip() == "f":
        keyboard.press("f")

    if data.decode().strip() == "!f":
        keyboard.release("f")

Arduino Code

Arduino
void setup(){
 pinMode(2, INPUT); 
 pinMode(3, INPUT);
 pinMode(4, INPUT); 
 Serial.begin(9600);        
}

void loop(){
    if (digitalRead(2) == HIGH) {
      Serial.println("d");
    }

    else {
      Serial.println("!d");
    }

    if (digitalRead(3) == HIGH) {
      Serial.println("a");
    }

    else {
      Serial.println("!a");
    }

    if (digitalRead(4) == HIGH) {
      Serial.println("f");
    }

    else {
      Serial.println("!f");
    }
    delay(50);
}

Credits

noah_arduino
1 project • 5 followers
Contact

Comments

Please log in or sign up to comment.