Translate

Mengakses Joystick Playstation 2 (PS2) Menggunakan ARDUINO dan LCD I2C

Mengakses Joystick Playstation 2 (PS2) Menggunakan ARDUINO dan LCD I2C


        Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan joystick PS2 sebagai kendalinya untuk ditampilkan pada LCD Arduino, jadi sistem kali ini hanya menampilkan karakter tombol yang ditekan pada joystick misalkan menekan tombol kotak joystick maka pada layar LCD Arduiho akan bertuliskan "kotak" begitu seterusnya, aplikasi untuk sistem ini sangatlah banyak antara lain kendali robot menggunakan joystick atau alat pemidah barang menggunakan joystick dan masih bayak yang lainnya. untuk program dan skemanya seperti berikut.


Library Joystick DOWNLOAD



 a. Arduino UNO






b. Joystick PS2







c. LCD + I2C






d. Wiring Diagram







e. Program Arduino IDE

#include <Wire.h>  // i2C Conection Library
#include <PS2X_lib.h>                         /* PS2 Controller Library */
#include <LiquidCrystal_I2C.h>  //i2C LCD Library
LiquidCrystal_I2C lcd(0x3F, 16, 2);

PS2X ps2x;                                    /* create PS2 Controller Class*/
byte Type = 0;
byte vibrate = 0;
int RX=0,RY=0,LX=0,LY=0;

int x = 0;
int y = 0;
int z = 0;

void setup(){
   lcd.begin();
   lcd.noCursor();
   lcd.clear();
   Serial.begin(9600);
   ps2x.config_gamepad(13,11,10,12, true, true);
   /* setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error*/
   Type = ps2x.readType();                       /* Reading type of the PS2 Ccontroller */
   if(Type==1){                                  /* Type 1 is Duel shock controller */ 
   }
}
void loop(){
  
   ps2x.read_gamepad(false, vibrate);   /* read controller and set large motor to spin at 'vibrate' speed */
   LY = ps2x.Analog(PSS_LY);          /* Reading Left stick Y axis */
   LX = ps2x.Analog(PSS_LX);          /* Reading Left stick X axis */
   RY = ps2x.Analog(PSS_RY);          /* Reading Right stick Y axis */
   RX = ps2x.Analog(PSS_RX);          /* Reading Right stick X axis */
   if((LY <= 9))                      /* standardize to 3 digit by checking less than 10 */
      //Serial.print("00");                /* eg: if LY= 5 then it display as "005" in lcd */
   if((LY >= 9 &&LY <= 99))           /* standardize to 3 digit by checking between 10-99 */
      //Serial.print("0");                 /* eg: if LY= 55 then it display as "055" in lcd */    
   //Serial.println(LY,DEC);                 /* display left analog stick Y axis */
      //lcd.print(",");                    /* separate values using comma */
   if((LX <= 9))                      /* standardize to 3 digit by checking less than 10 */
      //lcd.print("00");                /* eg: if LX= 5 then it display as "005" in lcd */
   if((LX >= 9 && LX<=99))            /* standardize to 3 digit by checking between 10-99 */
      //lcd.print("0");                 /* eg: if LX= 55 then it display as "055" in lcd */ 
   //Serial.println(LX,DEC);                 /* display left analog stick X axis */
      //lcd.print(",");                    /* separate values using comma */
   if((RY <= 9))                      /* standardize to 3 digit by checking less than 10 */
      //lcd.print("00");                /* eg: if RY= 5 then it display as "005" in lcd */
   if((RY >= 9 &&RY<=99))             /* standardize to 3 digit by checking between 10-99 */
      //lcd.print("0");                 /* eg: if RY= 55 then it display as "055" in lcd */ 
   //Serial.println(RY,DEC);                 /* display Right analog stick Y axis               */
      //lcd.print(",");                    /* separate values using comma */
   if((RX <= 9))                      /* standardize to 3 digit by checking less than 10 */
      //lcd.print("00");                /* eg: if RX= 5 then it display as "005" in lcd */
   if((RX >= 9 &&RX <= 99))           /* standardize to 3 digit by checking between 10-99 */
                                      /* eg: if RX= 55 then it display as "055" in lcd */ 
   Serial.println(RX,DEC);                 /* display Right analog stick X axis          */
   if(ps2x.NewButtonState()) {        /* will be TRUE if any button changes state  */
      if(ps2x.Button(PSB_START))      /* will be TRUE as long START button is pressed */
         y = y + 1;
      if(ps2x.Button(PSB_SELECT))             /* will be TRUE as long SELECT button is pressed */
         y = y + 2;
      if(ps2x.Button(PSB_PAD_UP))             /* will be TRUE as long as UP button is pressed */
         y = y + 3;
      if(ps2x.Button(PSB_PAD_RIGHT))          /* will be TRUE as long as UP button is pressed */
         y = y + 4;
      if(ps2x.Button(PSB_PAD_LEFT))           /* will be TRUE as long as LEFT button is pressed */
         y = y + 5;
      if(ps2x.Button(PSB_PAD_DOWN))           /* will be TRUE as long as DOWN button is pressed */
         y = y + 6;
      if(ps2x.Button(PSB_L1))                 /* will be TRUE as long as L1 button is pressed */
         z = z + 1;
      if(ps2x.Button(PSB_R1))                 /* will be TRUE as long as R1 button is pressed */
         z = z + 2;
      if(ps2x.Button(PSB_L2))                 /* will be TRUE as long as L2 button is pressed */
         z = z + 3;
      if(ps2x.Button(PSB_R2))                 /* will be TRUE as long as R2 button is pressed */
         z = z + 4;
      if(ps2x.Button(PSB_L3))                 /* will be TRUE as long as L3 button is pressed */
         z = z + 5;
      if(ps2x.Button(PSB_R3))                 /* will be TRUE as long as R3 button is pressed */
         z = z + 6;
      if(ps2x.Button(PSB_GREEN))              /* will be TRUE as long as GREEN/Triangle button is pressed */
         x = x + 1;
      if(ps2x.Button(PSB_BLUE))                /* will be TRUE as long as BLUE/CROSS/X button is pressed */
         x = x + 2;
       if(ps2x.Button(PSB_RED))               /* will be TRUE as long as RED/Circle button is pressed */
         x = x + 7;
      if(ps2x.Button(PSB_PINK))               /* will be TRUE as long as PINK/Squre button is pressed */
         x = x + 4;
   
      //tombol x = 6 dan 2
      //tombol kotak = 4
      //tombol segitiga = 8 dan 1
      //tombol bulat = 7 dan 9
     
      //atas = 9 dan 5
      //kiri = 7
      //bawah = 8 dan 13
      //kanan = 12 dan 6
     
      lcd.setCursor(0,0);
      if((x == 6)||(x == 2)){
      lcd.print("Tombol X        ");
      }
      if(x == 4){
      lcd.print("KOTAK           ");
      }
      if((x == 8)||(x == 1)){
      lcd.print("SEGITIGA        ");
      }
      if((x == 7)||(x == 9)){
      lcd.print("BULAT           ");
      }
     
     
     
      lcd.setCursor(0,1);
      if((y == 9)||(y == 5)){
      lcd.print("ATAS            ");
      }
      if(y == 7){
      lcd.print("KIRI            ");
      }
      if((y == 8)||(y == 13)){
      lcd.print("BAWAH           ");
      }
      if((y == 12)||(y == 6)){
      lcd.print("KANAN           ");
      }
     
      //Serial.print("X=");
      //Serial.println(x);
      //Serial.print("Y=");
      //Serial.println(y);
      //Serial.print("Z=");
      //Serial.println(z);
     
      delay(200);
     
      x = 0;
      y = 0;
      z = 0;
     
   }
   else;
      x = 0;
      y = 0;
      z = 0;
}






f. VIDEO HASILNYA









   

No comments:

Post a Comment