Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
| ||||||
|
The story is about when I wanted to create a game controller using arduino.
I had a problem that I could not find any tutorials with a library for arduino Uno so I resolved to youtube. Spent Hours and hours searching, finally found one. I used its code for 2-3 days and then decided to modify it. it modified it to use 2 external pushbuttons ( 1 is Work In Progress ) so I have decided to share it out. ( it works ). Please find Components and software in respective places.
const int JoyStick_pin = 8; //plug Joystick 'Button' into pin 8
const int X_pin = A0; //plug joystick X direction into pin A0
const int Y_pin = A1; //plug joystick Y direction into pin A1
int xc;
int yc;
int JSButton;
void setup() {
for (int i = 0; i < 2; i++) {
pinMode(JoyStick_pin, INPUT);
}
Serial.begin(115200);
Serial.print(X_pin);
Serial.println(Y_pin);
}
void loop() {
int x = analogRead(X_pin) - 521; //read x direction value and -517 to bring back to around 0
int y = analogRead(Y_pin) - 528; //read y direction value and -512 to bring back to around 0
if (x <-10) { //joystick has off set of +/-8 so this negates that
xc = 0; //turn analogue value into integer. 0, 1 or 2 depending on state
} else if (x >10) {
xc = 2;
} else {
xc = 1;
}
if (y < -10) {
yc = 0;
} else if (y >10) {
yc = 2;
} else {
yc = 1;
}
int buttonStates = analogRead(A5); //set starting value of Joystick button
if (buttonStates > 0) {
buttonStates = 1;
} else {
buttonStates = analogRead(A5);
}
Serial.print("S"); //start printing the data, format is Sxc,yc,buttonStates > S1,1,0
Serial.print(xc);
Serial.print(",");
Serial.print(yc);
Serial.print(",");
Serial.println((buttonStates));
delay(40);
}
Python IDLE code
PythonGo to idle after installing python from link click on file select new copy paste code and save to your preferred location then click on run and run module . it will run. make sure not to have serial monitor opened. the enjoy socwars on crazygames or customized keys for your own uses
import serial # import using pip
import pydirectinput
arduino = serial.Serial('COM3', 115200, timeout=.1) #serial input from arduino. change COM port to wherever your arduino is connected
pydirectinput.PAUSE = 0
keysDown = {} #list of currently pressed keys
def keyDown(key): #what to do if key pressed. takes value from handleJoyStickAsArrowKeys
keysDown[key] = True #adds key to KeysDown list
pydirectinput.keyDown(key) #runs pydirectinput using key from (argument)
#print('Down: ', key) #remove '#' from print to test data stream
def keyUp(key): #what to do if key released. takes value from handleJoyStickAsArrowKeys
if key in keysDown:
del (keysDown[key]) #remove key from KeysDown
pydirectinput.keyUp(key) #runs pydirectinput using key from (argument)
#print('Up: ', key) #remove '#' from print to test data stream
def handleJoyStickAsArrowKeys(x, y, z): #note that the x and y directions are swapped due to the way I orient my thumbstick
if x == 0: #0 is up on joystick
keyDown('down') #add up key to keyDown (argument)
keyUp('up') #add down key to keyUp (argument), as you can't press up and down together
elif x == 2: #2 is down on joystick
keyDown('up')
keyUp('down')
else: #1 is neutral on joystick
keyUp('up')
keyUp('down')
if y == 2: #2 is right on joystick
keyDown('right')
keyUp('left')
elif y == 0: #0 is left on joystick
keyDown('left')
keyUp('right')
else: #1 is neutral on joystick
keyUp('left')
keyUp('right')
if z == 1: #z argument is JSButton in this case. 1 is button pressed
keyDown('space') #key to be pressed with Joystick button. Change to any key
else:
keyUp('space') #0 is button not pressed
while True:
rawdata = arduino.readline() #read serial data from arduino one line at a time
data = str(rawdata.decode('utf-8')) #decode the raw byte data into UTF-8
if data.startswith("S"): #make sure the read starts in the correct place
dx = int(data[1]) #X direction is second digit in data (data[0] is 'S')
dy = int(data[3]) #Y direction is fourth digit in data
JSButton = int(data[5]) #JSButton is sixth digit in data
#print(dx, dy, JSButton) #remove '#' from print to test data stream
handleJoyStickAsArrowKeys(dx, dy, JSButton) #run body of code using dx, dy and JSButton as inputs
Comments