Arduino Game Pad part 1

All gamers have a dream to create their own game and make money with that (or just playing it), in this post I’ll show you a very simple arcanoid-type game but playing it your you own gamepad.

Ok I’ll not create a game but a gamepad, it still cool! The gamepad I’ll make it is based on arduino board  and ATMEGA328 microprocessor. In this part I will show you the hardware side and in the next part I’ll connect this to the “game”.

First of all what do we need :

– An Arduino board

– 3 buttons (for left – right – hit)

– A piezo (for sound when you hit)

– 9 Cables (to connect the components)

– 3 Resistors ( for the button’s potential)

– And on vibrator , I take it out from a ps2 gamepad, and I used “durex vibrating ring” before, yes I did. ( It vibrates when losing)

So the circuit connection is the following :

(Click image for full)

My implementation :

So as I create the connection it’s time to upload my Arduino sketch. I’ll use read-write via serial for sending the protocol command which are : ‘L’ for Left ,’R’ for Right (read from arduino) and ‘L’ for Lose ‘W’ for Win (write to arduino). It follows the sketch code :

int button_L = 2;
int button_R = 4; 
int button_Hit = 10; 
int buttonstate_L = 0;
int buttonstate_R = 0;
int buttonstate_Hit = 0;

int vib_Pin = 6;
int speaker = 8;

int incomingByte;
//notes freq more on another post
int Cur_tone[15] = {1915,1915,1915,1915,1915,1700,1700,1700,1700,1700,1519,1519,1519,1519,1519};

void setup() 
{
  // initialize the serial communication:
 Serial.begin(9600); 

 pinMode(button_L,INPUT); 
 pinMode(button_R,INPUT);

 pinMode(vib_Pin, OUTPUT); 
 pinMode(speaker, OUTPUT);

} //end setup

void loop() 
{
  buttonstate_L = digitalRead(button_L);
  buttonstate_R = digitalRead(button_R);
  buttonstate_Hit = digitalRead(button_Hit);

  if (Serial.available() > 0) 
  {
     // read the oldest byte in the serial buffer:
     incomingByte = Serial.read();

     if (incomingByte == 'L') //Lose and vibrate 
     {
       digitalWrite(vib_Pin, HIGH);
       delay(1000);
       digitalWrite(vib_Pin, LOW);
       incomingByte = 'N';
     }

     if (incomingByte == 'W') //Win and play sound 
     {
       for( int i=0;i < 15;i++)
       {
         digitalWrite(speaker, HIGH);
         delayMicroseconds(Cur_tone[i]);
         digitalWrite(speaker, LOW);
         delayMicroseconds(Cur_tone[i]);
       }
     }
  } 

  if(buttonstate_L == HIGH)
	Serial.println('L');
  if(buttonstate_R == HIGH)
	Serial.println('R');
  if(buttonstate_Hit == HIGH)
	Serial.println('H');

  delay(10);
}// end loop

Time to upload the code and test the circuit connection, after done uploading the code with Arduino enviroment , open serial screen to see if you receive and send command via the serial port.

When you press the ‘L’eft and ‘R’ight buttons you have to see something like this :


same for  the ‘H’it button.

So as we see you can receive data from arduino to your PC, now you have to send data from PC to arduino, try to send ‘L’ and ‘W’. On ‘L’ you have to “feel” the vibration and on ‘W’ you have to hear a sound.

When everthing works perfectly, it’s time to wait for the part 2 where you’ll see how to connect the gamepad to a Borland C++ 6 application (simple arcanoid game), with a class that can success the serial port communication.

See you on part 2!

Advertisements

One thought on “Arduino Game Pad part 1

  1. Pingback: Arduino Game Pad part 2 « Godspeech's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s