Controlling Game with arduino UNO!! with python
We will Learn how to control a Game with joystick ( 1 ) plus 1 button ( 2nd currently in progress.
Components and supplies
1
Analog joystick (Generic)
1
Pushbutton switch 12mm
1
Arduino UNO
Apps and platforms
1
Arduino IDE
1
Python Idle
1
Arduino Web Editor
Project description
Code
IDE editor code
arduino
1const int JoyStick_pin = 8; //plug Joystick 'Button' into pin 8 2const int X_pin = A0; //plug joystick X direction into pin A0 3const int Y_pin = A1; //plug joystick Y direction into pin A1 4int xc; 5int yc; 6int JSButton; 7 8void setup() { 9 for (int i = 0; i < 2; i++) { 10 pinMode(JoyStick_pin, INPUT); 11 } 12 13 Serial.begin(115200); 14 Serial.print(X_pin); 15 Serial.println(Y_pin); 16} 17 18void loop() { 19 int x = analogRead(X_pin) - 521; //read x direction value and -517 to bring back to around 0 20 int y = analogRead(Y_pin) - 528; //read y direction value and -512 to bring back to around 0 21 22 if (x <-10) { //joystick has off set of +/-8 so this negates that 23 xc = 0; //turn analogue value into integer. 0, 1 or 2 depending on state 24 } else if (x >10) { 25 xc = 2; 26 } else { 27 xc = 1; 28 } 29 30 if (y < -10) { 31 yc = 0; 32 } else if (y >10) { 33 yc = 2; 34 } else { 35 yc = 1; 36 } 37 38 int buttonStates = analogRead(A5); //set starting value of Joystick button 39 if (buttonStates > 0) { 40 buttonStates = 1; 41 } else { 42 buttonStates = analogRead(A5); 43 } 44 Serial.print("S"); //start printing the data, format is Sxc,yc,buttonStates > S1,1,0 45 Serial.print(xc); 46 Serial.print(","); 47 Serial.print(yc); 48 Serial.print(","); 49 Serial.println((buttonStates)); 50 51 delay(40); 52 53}
Python IDLE code
python
Go 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
1import serial # import using pip 2import pydirectinput 3 4arduino = serial.Serial('COM3', 115200, timeout=.1) #serial input from arduino. change COM port to wherever your arduino is connected 5 6pydirectinput.PAUSE = 0 7 8keysDown = {} #list of currently pressed keys 9 10 11def keyDown(key): #what to do if key pressed. takes value from handleJoyStickAsArrowKeys 12 keysDown[key] = True #adds key to KeysDown list 13 pydirectinput.keyDown(key) #runs pydirectinput using key from (argument) 14 #print('Down: ', key) #remove '#' from print to test data stream 15 16 17def keyUp(key): #what to do if key released. takes value from handleJoyStickAsArrowKeys 18 if key in keysDown: 19 del (keysDown[key]) #remove key from KeysDown 20 pydirectinput.keyUp(key) #runs pydirectinput using key from (argument) 21 #print('Up: ', key) #remove '#' from print to test data stream 22 23 24def handleJoyStickAsArrowKeys(x, y, z): #note that the x and y directions are swapped due to the way I orient my thumbstick 25 if x == 0: #0 is up on joystick 26 keyDown('down') #add up key to keyDown (argument) 27 keyUp('up') #add down key to keyUp (argument), as you can't press up and down together 28 elif x == 2: #2 is down on joystick 29 keyDown('up') 30 keyUp('down') 31 else: #1 is neutral on joystick 32 keyUp('up') 33 keyUp('down') 34 35 if y == 2: #2 is right on joystick 36 keyDown('right') 37 keyUp('left') 38 elif y == 0: #0 is left on joystick 39 keyDown('left') 40 keyUp('right') 41 else: #1 is neutral on joystick 42 keyUp('left') 43 keyUp('right') 44 45 if z == 1: #z argument is JSButton in this case. 1 is button pressed 46 keyDown('space') #key to be pressed with Joystick button. Change to any key 47 else: 48 keyUp('space') #0 is button not pressed 49 50 51while True: 52 rawdata = arduino.readline() #read serial data from arduino one line at a time 53 data = str(rawdata.decode('utf-8')) #decode the raw byte data into UTF-8 54 if data.startswith("S"): #make sure the read starts in the correct place 55 dx = int(data[1]) #X direction is second digit in data (data[0] is 'S') 56 dy = int(data[3]) #Y direction is fourth digit in data 57 JSButton = int(data[5]) #JSButton is sixth digit in data 58 #print(dx, dy, JSButton) #remove '#' from print to test data stream 59 handleJoyStickAsArrowKeys(dx, dy, JSButton) #run body of code using dx, dy and JSButton as inputs 60
IDE editor code
arduino
1const int JoyStick_pin = 8; //plug Joystick 'Button' into pin 8 2const int X_pin = A0; //plug joystick X direction into pin A0 3const int Y_pin = A1; //plug joystick Y direction into pin A1 4int xc; 5int yc; 6int JSButton; 7 8void setup() { 9 for (int i = 0; i < 2; i++) { 10 pinMode(JoyStick_pin, INPUT); 11 } 12 13 Serial.begin(115200); 14 Serial.print(X_pin); 15 Serial.println(Y_pin); 16} 17 18void loop() { 19 int x = analogRead(X_pin) - 521; //read x direction value and -517 to bring back to around 0 20 int y = analogRead(Y_pin) - 528; //read y direction value and -512 to bring back to around 0 21 22 if (x <-10) { //joystick has off set of +/-8 so this negates that 23 xc = 0; //turn analogue value into integer. 0, 1 or 2 depending on state 24 } else if (x >10) { 25 xc = 2; 26 } else { 27 xc = 1; 28 } 29 30 if (y < -10) { 31 yc = 0; 32 } else if (y >10) { 33 yc = 2; 34 } else { 35 yc = 1; 36 } 37 38 int buttonStates = analogRead(A5); //set starting value of Joystick button 39 if (buttonStates > 0) { 40 buttonStates = 1; 41 } else { 42 buttonStates = analogRead(A5); 43 } 44 Serial.print("S"); //start printing the data, format is Sxc,yc,buttonStates > S1,1,0 45 Serial.print(xc); 46 Serial.print(","); 47 Serial.print(yc); 48 Serial.print(","); 49 Serial.println((buttonStates)); 50 51 delay(40); 52 53}
Downloadable files
Schematic
Sorry for bad drawing
Schematic

Schematic
Sorry for bad drawing
Schematic

Comments
Only logged in users can leave comments