Skip to content

Instantly share code, notes, and snippets.

@fxprime
Last active August 21, 2024 12:45
Show Gist options
  • Save fxprime/62387a63e3e444df7defae1484d941bd to your computer and use it in GitHub Desktop.
Save fxprime/62387a63e3e444df7defae1484d941bd to your computer and use it in GitHub Desktop.
/**
ตัวอย่างการใช้ PS2 Wireless กับ Arduino
PS2X_lib
ในการอ่านค่า joy PS2 ของร้าน Modulemore
โดยต่อ
UNO R3 --- > PS2 adapter
GND - GND
5v - VCC
D3 - DAT
D4 - CMD
D5 - CS
D6 - CLK
** ต่อไฟเลี้ยงให้ตัวรับก่อนตัวส่ง(จอย)ทุกครั้ง
ทดสอบ joy analog ให้กด L1 หรือ R1 ค้างไว้
ทดสอบการสั่นให้กดปุ่ม X ค้างไว้ (บางจอยจะไม่มีระบบการสั่น ให้ใช้ Rumble = false)
Product Link : https://www.modulemore.com/product/538
**/
#include <PS2X_lib.h> //for v1.6
PS2X ps2x;
int error = -1;
byte type = 0;
byte vibrate = 0;
void setup() {
Serial.begin(57600);
while (error != 0) {
//setup pins and settings: GamePad(CLK, CMD, CS, DAT, Pressures?, Rumble?)
error = ps2x.config_gamepad(6, 4, 5, 3, false, true);
if (error == 0) {
Serial.println("Found Controller, configured successful");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Go to www.billporter.info for updates and to report bugs.");
}
else if (error == 1) Serial.println("No controller found, check wiring.");
else if (error == 2) Serial.println("Controller found but not accepting commands.");
else if (error == 3) Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch (type) {
case 0:
Serial.println("Unname Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
case 2:
Serial.println("GuitarHero Controller Found (Not implement here)");
break;
}
delay(100);
}
}
void loop() {
if (error == 1) //skip loop if no controller found
return;
if(type==1) { //DualShock Controller
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if (ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if (ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if (ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if (ps2x.Button(PSB_PAD_RIGHT)) {
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if (ps2x.Button(PSB_PAD_LEFT)) {
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if (ps2x.Button(PSB_PAD_DOWN)) {
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
{
if (ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if (ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if (ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if (ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if (ps2x.Button(PSB_GREEN))
Serial.println("Triangle pressed");
}
if (ps2x.ButtonPressed(PSB_RED)) //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
if (ps2x.ButtonReleased(PSB_PINK)) //will be TRUE if button was JUST released
Serial.println("Square just released");
if (ps2x.NewButtonState(PSB_BLUE)) //will be TRUE if button was JUST pressed OR released
{
Serial.println("X just changed");
if (ps2x.ButtonReleased(PSB_BLUE)) {
vibrate = 0;
Serial.println("X release, stop vibrate");
} else {
vibrate = 255;
Serial.println("X press, start vibrate");
}
}
if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
{
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
}
}
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment