STEMpedia
Published © CC BY

DIY Touch Based Game Controller

This project will show you how to make a DIY game controller.

IntermediateFull instructions provided3 hours755
DIY Touch Based Game Controller

Things used in this project

Hardware components

evive
STEMpedia evive
×1
USB-A to B Cable
USB-A to B Cable
×1
Thick Cardboard
×1
Male/Male Jumper Wires
×1
Glue Gun and Glue Sticks
×1

Software apps and online services

Arduino IDE
Arduino IDE
Processing
The Processing Foundation Processing

Story

Read more

Schematics

Circuit Diagram for the connection of Gamepad

Code

Arduino Code for Touch Controller

Arduino
#include <Wire.h>
#include <Adafruit_MPR121.h>

Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t currenttouched = 0;
const uint16_t wait = 100;
int var =0;


void setup()
    {
       // put your setup code here, to run once:

      Serial.begin(9600);

      if (!cap.begin(0x5A))
          {
            Serial.println("MPR121 not found, check wiring?");
            while (1);
          }
 
   }

void loop()
     {
         // put your main code here, to run repeatedly:
         currenttouched = cap.touched();
         var =0;
         //-------------------------SW1-----------------------------
         if(currenttouched & _BV(0)) //  up
             {
               var = 1;
               Serial.print(var);
               Serial.print(".");
               delay(wait);
             }
 
        //-----------------------SW2--------------------------------
        if( currenttouched & _BV(1))  // down
            {
              var = 2;
              Serial.print(var);
              Serial.print(".");
              delay(wait);
            }
  
        //----------------------------SW3---------------------------
       if( currenttouched & _BV(2))  // right
           {
             var = 3;
             Serial.print(var);
             Serial.print(".");
             delay(wait);
           }

       //---------------------------SW4------------------------------
       if( currenttouched & _BV(3))  // left 
          {
            var = 4;
            Serial.print(var);
            Serial.print(".");
            delay(wait);
          }
      //--------------------------SW5-----------------------------
     if(currenttouched & _BV(4))  // space
         {
           var = 5;
           Serial.print(var);
           Serial.print(".");
           delay(wait);
         }
    //-------------------------SW6--------------------------
    if(currenttouched & _BV(6))   // shift
        {
          var = 6;
          Serial.print(var);
          Serial.print(".");
          delay(wait);
        }
     // ----------------------SW7---------------------------
    if(currenttouched & _BV(7)) //  enter
       {
         var = 7;
         Serial.print(var);
         Serial.print(".");
         delay(wait);
       }
    else
       {
         Serial.print(var);
         Serial.print(".");
       }
 
  }

Processing Code for Touch Controller

Processing
import processing.serial.*; // imports library for serial communication
import java.awt.Robot; // imports library for key press or release simulation
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;

Serial port; // defines Object Serial
Robot robot; // defines Object Robot

String data;
int iv=0;

void setup()
  {

    try 
       {
          robot = new Robot();
       }
    catch (Exception e)
       {
          e.printStackTrace();
          exit();
       }

    delay(2000);
    size (800, 800);
    port = new Serial(this,"COM59", 9600); // starts the serial communication
    port.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: 215,214/141;315:314<316!314?315.
    background(0,0,0);
 }

void draw()
  {
    print("data:");
    print(data);

    print(" v= ");
    println(iv);

    if(iv == 1)
       {
         robot.keyPress(KeyEvent.VK_UP);
         print(" UP "); 
       }

    else if(iv ==2)
      {   
        robot.keyPress(KeyEvent.VK_DOWN);
        print(" DOWN ");
      }
   else if(iv == 4)
     {
       robot.keyPress(KeyEvent.VK_LEFT);
       print(" LEFT ");
     }
   else if(iv == 3)
     {
        robot.keyPress(KeyEvent.VK_RIGHT);
        print(" RIGHT ");  
     }

   else if(iv==6)
     {
        robot.keyPress(KeyEvent.VK_SHIFT);
        print("SHIFT");
     }
   else if(iv==5)
      {
        robot.keyPress(KeyEvent.VK_SPACE);
        print(" SPACE ");
      }
   else if(iv==7)
     {
       robot.keyPress(KeyEvent.VK_ENTER);
       print(" ENTER ");
     }
   else if(iv==0)
     {
       robot.keyRelease(KeyEvent.VK_UP);
       robot.keyRelease(KeyEvent.VK_DOWN);
       robot.keyRelease(KeyEvent.VK_LEFT);
       robot.keyRelease(KeyEvent.VK_RIGHT);
       robot.keyRelease(KeyEvent.VK_A);
       robot.keyRelease(KeyEvent.VK_S);
       robot.keyRelease(KeyEvent.VK_W);
       robot.keyRelease(KeyEvent.VK_D);
       robot.keyRelease(KeyEvent.VK_SPACE);
       robot.keyRelease(KeyEvent.VK_ENTER);
       robot.keyRelease(KeyEvent.VK_SHIFT);  
     } 
 
 }

void serialEvent (Serial port) // starts reading data from the Serial Port
   {

      data = port.readStringUntil('.'); // reads the data from the serial port up to the character '.' and it sets that into the String variable "data". So actually it reads this: 215,214/141;315:314<316!314?315.
      data = data.substring(0,data.length()-1); // it removes the '.' from the previous read. So this will be the String "data" variable: 215,214/141;315:314<316!314?315

      // Finding the indexes in the data and setting the variables from the sensors by taking from the String "data" the appropriate values that are between the characters in the "data" String

     iv= int(data);
   }

evive Library

C/C++
No preview (download only).

Credits

STEMpedia

STEMpedia

42 projects • 169 followers
STEMpedia blends theory with experiential learning by offering state-of-the-art technology, projects, tutorials, courses, and much more.

Comments