Arduino Game Pad part 2

In  the previous post  we created the hardware for an arduino gamepad. Now it’s time to connect it to a simple rough game just to see that it works.

First of all we need a library that can success the serial communication for trading data between our application and arduino. I used “Tserial” library you can download it from here (both .h and .cpp files). I create this very-simple-arcanoid on Borland C++ 6 framework.

There is a void SerialEventManager(uint32 object, uint32 event) function in my code for manage events from serial port, keep logs and send data back. So here is the implementation :

void SerialEventManager(uint32 object, uint32 event)
{
    char *buffer;
    int   size;
    Tserial_event *com;
    char   txt[512];

    com = (Tserial_event *) object;
    if (com!=0)
    {
        switch(event)
        {
         case SERIAL_CONNECTED  :
               Fmain->Memo1->Lines->Append(AnsiString("Connected !"));
               break;
         case SERIAL_DISCONNECTED  :
              main->Memo1->Lines->Append(AnsiString("Disonnected !"));
              break;
         case SERIAL_DATA_SENT  :
              //OnDataSent();
              Fmain->Memo1->Lines->Append(AnsiString("Data sent !"));
              break;
          case SERIAL_DATA_ARRIVAL  :
               size = com->getDataInSize();
               buffer = com->getDataInBuffer();

               if (size>511)
               size=511;

               memcpy(txt,buffer, size);
               txt[size] = 0;
               Fmain->Memo1->Clear();
               for(int i = 0;i < AnsiString(txt).Length();i++)                         {                                                                         if( (AnsiString(txt[i]) == 'R') )                                          Fmain->Memo1->Text = "R";
                 if( (AnsiString(txt[i]) == 'L') )
                    Fmain->Memo1->Text = "L";
                 if( (AnsiString(txt[i]) == 'H') )
                    Fmain->Memo1->Text = "H";
                }
                com->dataHasBeenRead();
                break;
        }
    }
}

The code to connect / disconnect serial port with buttons :

void __fastcall TFmain::ButtonDisconnectClick(TObject *Sender)
{
    serial_port.disconnect();
}

void __fastcall TFmain::ButtonConnectClick(TObject *Sender)
{
    // Connect to COM3 
    serial_port.connect("COM3",9600,SERIAL_PARITY_NONE,8,false);
}

On the main program there just a timer that moves a ball around the form’s coordinates and we can move a box on the bottom side of the screen to protect not to fall the ball under it. The code in the timer function is bellow :

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Shape1->Top += dy;    // the steps
Shape1->Left += dx;
if(Shape1->TopLeftShape1->Top + Form1->Shape1->Height) == (Form1->Shape2->Top))       // To the box
 && ((Form1->Shape1->Left >= (Form1->Shape2->Left-20))
 && (Form1->Shape1->Left Shape2->Left+Form1->Shape2->Width)) )

   {
   dy = -dy;
   Fmain->Edit1->Text = "W";
   Fmain->Button2->Click();
   }

if(Shape1->Top + Shape1->Height > Form1->ClientHeight-1)   //Down
  {
  loses ++;
  Label2->Caption = loses;
  Shape1->Top = 40;
  Shape1->Left = (Form1->ClientWidth-Shape1->Width)/2;

  Fmain->Edit1->Text = 'L';
  Fmain->Button2->Click();
  }
if(Shape1->Left + Shape1->Width > Form1->ClientWidth-1) dx = -dx;   //Left

if(Fmain->flag_hit)
 {
   if( (Shape3->Top == Shape1->Top+Shape1->Height)
   && (Shape3->Left >= Shape1->Left)
   && (Shape3->Left Left+Shape1->Width) )
      {
      Fmain->Edit1->Text = "WWW";
      Fmain->Button2->Click();
      Fmain->flag_hit = false;
      Shape3->Visible = false;
      hits++;
      }
   if(Shape3->Top > Form1->Top)
       Shape3->Top -= hy;
   else
       {
        Fmain->flag_hit = false;
        Shape3->Visible = false;
        }
 }
if( (hits >=2) && (hits Brush->Color = clGreen;
if( (hits >=5) && (hits Brush->Color = clAqua;
if( (hits >=8) && (hits Brush->Color = clRed;
if( hits > 10)
   {
   Shape1->Brush->Color = clGray;
   hits = 0;
   wins ++;
   Label4->Caption = wins;
   }
}

The command from / to serial port are kept in a memo box and when this memo changes :

  int xl =10,xr = 10;
  if(Fmain->Memo1->Text == 'L')
     if(Form1->Shape2->Left -10 > 0)                                            Form1->Shape2->Left -= xl;
  if(Fmain->Memo1->Text == 'R')
     if(Form1->Shape2->Left + Form1->Shape2->Width +3                           < Form1->ClientWidth-10) Form1->Shape2->Left += xr;
   if(Fmain->Memo1->Text == 'H')
     if(!flag_hit)
       {
        Form1->Shape3->Visible = true;
        Form1->Shape3->Left = Form1->Shape2->Left                                                     +(Form1->Shape2->Width/2);
        Form1->Shape3->Top = Form1->Shape2->Top - 30;
        flag_hit = true;
       }

It is a very noob and simple implementaion just to show you how to connect your programs with Arduino, of course you can create great things with that combo.

So lets run the program with Arduino connected on COM3.

This screen appears when the program is running is the option to connect the arduino and you can see the box that keeps the command.

When you press Connect GamePad button it runs the function

serial_port.connect("COM3",9600,SERIAL_PARITY_NONE,8,false);

When the game starts the screen is the following :

Where when you can move the box around with the Left – Right button on your arduino and you can shoot with the H. If you hit the ball the program send to the serial port command ‘W’ and piezo plays a sound, if you lose then it send ‘L’ command and vibrator moving.

That was a very simple and rough implementation of a game for using your own arduino gamepad. All you need is the hardware I describe in the previous post, a serial port communication  library and some developing knowledge that’s all.

Advertisements

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