Stik PS2 Wireless Connect Arduino
Stik PS2 Wireless Controller Connect Arduino
Wiring diagram
- Digital pin 10 => PS2 receiver pin 6 (ATT)
- Digital pin 11 => PS2 receiver pin 2 (CMD)
- Digital pin 12 => PS2 receiver pin 1 (DATA)
- Digital pin 13 => PS2 receiver pin 7 (CLK)
- 3.3V pin => PS2 receiver pin 5 (POWER)
- Gnd pin => PS2 receiver pin 4 (GROUND
Setup Arduino IDE
1. Download and install Arduino IDE latest version
2. Adding the libraries
ARDUINO CODE
#include <PS2X_lib.h> //DOWNLOAD LIBRARY IN DESCRIPTION VIDEO /* PS2 Controller Library */ PS2X ps2x; /* create PS2 Controller Class*/ byte Type = 0; byte vibrate = 0; int RX = 0, RY = 0, LX = 0, LY = 0; byte x = 0; byte y = 0; byte z = 0; const byte led1 = A0; const byte led2 = A1; const byte led3 = A2; const byte led4 = A3; //MOTORPIN 1&2 = MOTOR 1 PWM (PIN 5,6) //MOTORPIN 3&4 = MOTOR 4 (PIN 2,3) //MOTORPIN 5&6 = MOTOR 5 (PIN4,7) const byte motorpin[]={5,6,2,3,4,7}; byte count = 6; unsigned int nilai1 = 1, nilai2 = 1, nilai3 = 1, nilai4 = 1; void setup() { for (int x =0; x<count; x++) { pinMode(motorpin[x], OUTPUT); } for (int x =0; x<count; x++) { pinMode(motorpin[x], LOW); } pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); 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 (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 if ((x == 6) || (x == 2)) { Serial.println("Tombol X"); nilai1++; if(nilai1 %2 == 0){ digitalWrite(led1, HIGH); nilai1=0; } else{ digitalWrite(led1,LOW); } } if (x == 4) { Serial.println("KOTAK"); nilai2++; if(nilai2 %2 == 0){ digitalWrite(led2, HIGH); nilai2=0; } else{ digitalWrite(led2,LOW); } } if ((x == 8) || (x == 1)) { Serial.println("SEGITIGA"); nilai3++; if(nilai3 %2 == 0){ digitalWrite(led3, HIGH); nilai3=0; } else{ digitalWrite(led3,LOW); } } if ((x == 7) || (x == 9)) { Serial.println("BULAT"); nilai4++; if(nilai4 %2 == 0){ digitalWrite(led4, HIGH); nilai4=0; } else{ digitalWrite(led4,LOW); } } if (y == 3) { Serial.println("ATAS"); } if (y == 5) { Serial.println("KIRI"); } if ((y == 6) || (y == 12)) { Serial.println("BAWAH"); } if (y == 4) { Serial.println("KANAN"); } if (z == 1) { Serial.println("L1"); motor4maju(); } if (z == 2) { Serial.println("R1"); motor5maju(); } if (z == 3) { Serial.println("L2"); motor4mundur(); } if (z == 4) { Serial.println("R2"); motor5mundur(); } if (z == 5) { Serial.println("L3"); } if (z == 6) { Serial.println("R3"); } //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; } void motor4maju(){ digitalWrite(motorpin[3], HIGH); digitalWrite(motorpin[4], LOW); } void motor4mundur(){ digitalWrite(motorpin[3], LOW); digitalWrite(motorpin[4], HIGH); } void motor5maju(){ digitalWrite(motorpin[5], HIGH); digitalWrite(motorpin[6], LOW); } void motor5mundur(){ digitalWrite(motorpin[5], LOW); digitalWrite(motorpin[6], HIGH); }
Komentar
Posting Komentar