Saturday, December 10, 2022

Make Your Controllers For Any Game Using Arduino: DIY

By Raghavendran V, 3rd year, Department of Computer Science and Engineering at Sri Sairam Engineering College, Chennai

- Advertisement -
 

Implementing the game controller using Arduino

  1. Download Arduino IDE and Microsoft visual studio IDE

https://www.arduino.cc/en/main/software – Arduino IDE Download link

https://www.visualstudio.com/downloads – Microsoft visual studio Download link

  1. Download the starter kit (game controller serial port listener program) from the below link https://github.com/RaghavVenkat/Serial_Listiner_Game_Controller
  2. The starter pack has the code for Arduino (which implements the concept we discussed above) circuit diagrams and the base codes for the application that we are going to make using c# in Microsoft Visual studio.
  3. Connect the Ultrasonic sensor with arduino board. (use the above give circuit diagram, you can also find the circuit diagram inside the starter-kit).
  4. Connect the arduino board to PC. When you open IDE you will see two function setup() and loop()
  5. The setup() function will only run once, after each powerup or reset of the Arduino board. Use it to initialize/defining variables, pin modes, etc.
  6. The loop()function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino. arduino code for game controller using arduino
  7. As we have already discussed about the working of Ultrasonic sensor, let’s start with c# application designing part.
  8. Open Visual studio and open the serial_listiner c# program which you can find inside the “c# part” folder of the starter kit.
  9. Create two timers in the MainForm from the Toolbox panel.
  10. Set Enabled – as false and interval as 1, you can find theses under properties panel.

timer bindsource timer

- Advertisement -

timer properties

 

  1. Declare 2 variables

Int  or  long past =0;

Int or  long val =0;

  1. Navigate to void _spManager_NewSerialDataReceived(object sender,SerialDataEventArgs e) { ….. } function
  2. And type in the code give in the below picture.
  3. As we discussed we are triggering the keys based on the predicates that we have stated.
  4. Instead of directly triggering the keys we are going to use timers that inturn triggers keys so that it produces a considerable amount of effect on the game.

In the below code i am receiving the serial values are read as string and converted into int type.

  1. To convert string to int use Convert.ToInt32 or Convert.ToInt64, in case of 32 you need to catch type mismatch errors, but it will also work. If you are using Convert.ToInt64 then you need to declare the variables PAST and VAL with Long data type modifier.

Now let’s code the timer functionalities.

Here we are going to use one built-in function SendKeys.Send() – used for sending keystrokes to an application. This comes under Send keys class.

  1. Let us use timer 1 for sending F3 key when enabled and Timer 2 for sending F2 key when enabled respectively.
  • Send(“{F3}”); inside timer 1_tick function
  • Send(“{F2}”); inside timer 2_tick function

 

  1. Once after triggering a key we need to update the value of PAST variable to the current value read from the serial port.
  2. That’s all! Now run this application in parallel to the flight simulator game. Set the port, baud rate and hit on “Start Listening”.

In addition to this we can also add buttons, toggle switches to control landing lights, landing gears, etc. by treating them as interrupts and if you want to make controllers yoke/ joysticks used in airbus or Boeing, use flex resistors or gyro sensors (both fits good to do that functionality). Even in place of ultrasonic sensor we can also use potentiometer.

The below video of the project in action should also help,

This concept can be used for any games, for instance for a game like EURO TRUCK SIMULATOR we can use this setup as an accelerator and break by triggering the “Up arrow key”/ “W Key” and “DOWN arrow Key”/” S key” respectively. Thus, if you have an idea to create controller for any games,

  1. First find the action which you want to control from real world (here it refers to controller).
  2. Find the corresponding key that triggers that action in the game.
  3. Code the logic that is required to trigger this key.

Enjoy Gaming!


More projects available at: Electronics Projects

SHARE YOUR THOUGHTS & COMMENTS

 
 

What's New @ Electronicsforu.com?

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Tech Contests