How to make Arduino gamepad
Let's learn to make Arduino gamepad or controller to play games or even automate them
Components and supplies
2
Push button
1
Arduino UNO
2
Resistor 10k ohm
1
Jumper wires (generic)
Tools and machines
1
Breadboard, 170 Pin
Apps and platforms
1
Processing
1
Arduino IDE
Project description
Code
Arduino code
arduino
This is code for Arduino u have to play on Arduino ide
1const int UpButton = 9;//9 and 11 pins both only work as per circuit rest are not working as per circuit 2const int DownButton = 11; 3const int LeftButton = 10; 4const int RightButton = 8; 5void setup(){ 6 Serial.begin(9600); 7 pinMode(UpButton, INPUT); 8 pinMode(DownButton, INPUT); 9 pinMode(LeftButton, INPUT); 10 pinMode(RightButton, INPUT); 11} 12void loop(){ 13 if(digitalRead(UpButton) == HIGH){ 14 //for(int i; i < 10; i++){ (this code is for to click button once but run the player 10 times ps: delete this line only not for int etc u understand) 15 Serial.println("Up:"); 16 delay(1); 17 } 18 //} 19 if(digitalRead(DownButton) == HIGH){ 20 //for(int i; i < 10; i++){ 21 Serial.println("Down:"); 22 delay(1); 23 //break; 24 } 25 //} 26 if(digitalRead(LeftButton) == HIGH){ 27 //for(int i; i < 10; i++){ 28 Serial.println("Left:"); 29 delay(1); 30 //break; 31 } 32 //} 33 if(digitalRead(RightButton) == HIGH){ 34 //for(int i; i < 10; i++){ 35 Serial.println("Right:"); 36 delay(1); 37 //break; 38 } 39 //} 40}
Processing code
processing
This processing code edit with your needs
1import processing.serial.*; 2import java.awt.AWTException; 3import java.awt.Robot; 4import java.awt.event.InputEvent; 5import java.awt.event.KeyEvent; 6import javax.swing.KeyStroke; 7Serial MyPort; 8String KeyString = ""; 9void setup() 10{ 11 System.out.println("Hi"); 12 size(700, 500); 13 MyPort = new Serial(this, "COM3", 9600);// My Arduino is on COM3. Enter the COM on which your Arduino is on. 14 MyPort.bufferUntil('\ 15'); 16} 17void draw(){//Not really necessary 18 background(0, 0, 0); 19 fill(255, 10, 0); 20 text("Press any key this is non interactable screen open game or browser to test", 100, 175); 21} 22void serialEvent(Serial MyPort)throws Exception { 23 KeyString = MyPort.readStringUntil('\ 24'); 25 KeyString = KeyString.substring(0, KeyString.indexOf(':'));//The string is split. the whole string leaving the colon is taken 26 System.out.println(KeyString);//prints the serial string for debugging purpose 27 Robot Arduino = new Robot();//Constructor of robot class 28 switch(KeyString){ 29 case "Up" : 30 Arduino.keyPress(KeyEvent.VK_W);//presses up key. 31 delay(5); 32 Arduino.keyRelease(KeyEvent.VK_W);//releases up key 33 break; 34 case "Down" : 35 Arduino.keyPress(KeyEvent.VK_S);/// CHANGE ACCORIND TO YOUR NEEDS more info here https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html 36 delay(5); 37 Arduino.keyRelease(KeyEvent.VK_S); 38 break; 39 case "Right" : 40 Arduino.keyPress(KeyEvent.VK_D); 41 delay(5); 42 Arduino.keyRelease(KeyEvent.VK_D); 43 break; 44 case "LEFT" : 45 Arduino.keyPress(KeyEvent.VK_A); 46 delay(5); 47 Arduino.keyRelease(KeyEvent.VK_A); 48 break; 49 } 50 51}
Arduino code
arduino
This is code for Arduino u have to play on Arduino ide
1const int UpButton = 9;//9 and 11 pins both only work as per circuit rest 2 are not working as per circuit 3const int DownButton = 11; 4const int LeftButton 5 = 10; 6const int RightButton = 8; 7void setup(){ 8 Serial.begin(9600); 9 10 pinMode(UpButton, INPUT); 11 pinMode(DownButton, INPUT); 12 pinMode(LeftButton, 13 INPUT); 14 pinMode(RightButton, INPUT); 15} 16void loop(){ 17 if(digitalRead(UpButton) 18 == HIGH){ 19 //for(int i; i < 10; i++){ (this code is for to click button once 20 but run the player 10 times ps: delete this line only not for int etc u understand) 21 22 Serial.println("Up:"); 23 delay(1); 24 } 25 //} 26 if(digitalRead(DownButton) 27 == HIGH){ 28 //for(int i; i < 10; i++){ 29 Serial.println("Down:"); 30 31 delay(1); 32 //break; 33 } 34 //} 35 if(digitalRead(LeftButton) 36 == HIGH){ 37 //for(int i; i < 10; i++){ 38 Serial.println("Left:"); 39 40 delay(1); 41 //break; 42 } 43 //} 44 if(digitalRead(RightButton) 45 == HIGH){ 46 //for(int i; i < 10; i++){ 47 Serial.println("Right:"); 48 49 delay(1); 50 //break; 51 } 52 //} 53}
Processing code
processing
This processing code edit with your needs
1import processing.serial.*; 2import java.awt.AWTException; 3import java.awt.Robot; 4import java.awt.event.InputEvent; 5import java.awt.event.KeyEvent; 6import javax.swing.KeyStroke; 7Serial MyPort; 8String KeyString = ""; 9void setup() 10{ 11 System.out.println("Hi"); 12 size(700, 500); 13 MyPort = new Serial(this, "COM3", 9600);// My Arduino is on COM3. Enter the COM on which your Arduino is on. 14 MyPort.bufferUntil('\n'); 15} 16void draw(){//Not really necessary 17 background(0, 0, 0); 18 fill(255, 10, 0); 19 text("Press any key this is non interactable screen open game or browser to test", 100, 175); 20} 21void serialEvent(Serial MyPort)throws Exception { 22 KeyString = MyPort.readStringUntil('\n'); 23 KeyString = KeyString.substring(0, KeyString.indexOf(':'));//The string is split. the whole string leaving the colon is taken 24 System.out.println(KeyString);//prints the serial string for debugging purpose 25 Robot Arduino = new Robot();//Constructor of robot class 26 switch(KeyString){ 27 case "Up" : 28 Arduino.keyPress(KeyEvent.VK_W);//presses up key. 29 delay(5); 30 Arduino.keyRelease(KeyEvent.VK_W);//releases up key 31 break; 32 case "Down" : 33 Arduino.keyPress(KeyEvent.VK_S);/// CHANGE ACCORIND TO YOUR NEEDS more info here https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html 34 delay(5); 35 Arduino.keyRelease(KeyEvent.VK_S); 36 break; 37 case "Right" : 38 Arduino.keyPress(KeyEvent.VK_D); 39 delay(5); 40 Arduino.keyRelease(KeyEvent.VK_D); 41 break; 42 case "LEFT" : 43 Arduino.keyPress(KeyEvent.VK_A); 44 delay(5); 45 Arduino.keyRelease(KeyEvent.VK_A); 46 break; 47 } 48 49}
Downloadable files
Gamepad Schematics or how to connect wires to arduino
This is the circuit to connect wires to Arduino and breadboard so u have a gamepad ready
Gamepad Schematics or how to connect wires to arduino

Comments
Only logged in users can leave comments