PS2 Joy Adapter for Arduino อะแดปเตอร์แปลงหัว PS2 เป็นขาต่อแบบ SPI สำหรับ Arduino
- แปลงหัว JoyStick playstation PS2 สีดำ และ JoyStick playstation PS2 สีขาว ให้เป็นขาต่อสำหรับ Arduino
Arduino UNO R3 ต่อกับ อะแดปเตอร์แปลง PS2 เป็นขาต่อแบบ SPI
3.3V ต่อกับ 5V
GND ต่อกับ GND
10 ต่อกับ ATT
11 ต่อกับ CMD
12 ต่อกับ DATA
13 ต่อกับ CLK
Code ตัวอย่าง
1
#include PS2X_lib.h
3
PS2X ps2x;
5
int error = 0;
6
byte type = 0;
7
byte vibrate = 0;
9
void setup() {
10
Serial.begin(57600);
11
12
13
error = ps2x.config_gamepad(13, 11, 10, 12, true, true);
14
15
if (error == 0) {
16
Serial.println("Found Controller, configured successful");
17
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
18
Serial.println("holding L1 or R1 will print out the analog stick values.");
19
Serial.println("Go to www.billporter.info for updates and to report bugs.");
20
}
22
else if (error == 1)
23
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
25
else if (error == 2)
26
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
27
28
else if (error == 3)
29
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
30
31
//Serial.print(ps2x.Analog(1), HEX);
32
33
type = ps2x.readType();
34
switch (type) {
35
case 0:
36
Serial.println("Unknown Controller type");
37
break;
38
case 1:
39
Serial.println("DualShock Controller Found");
40
break;
41
case 2:
42
Serial.println("GuitarHero Controller Found");
43
break;
44
}
45
46
}
47
48
void loop() {
49
50
if (error == 1)
51
return;
52
53
if (type == 2) {
54
55
ps2x.read_gamepad();
56
57
if (ps2x.ButtonPressed(GREEN_FRET))
58
Serial.println("Green Fret Pressed");
59
if (ps2x.ButtonPressed(RED_FRET))
60
Serial.println("Red Fret Pressed");
61
if (ps2x.ButtonPressed(YELLOW_FRET))
62
Serial.println("Yellow Fret Pressed");
63
if (ps2x.ButtonPressed(BLUE_FRET))
64
Serial.println("Blue Fret Pressed");
65
if (ps2x.ButtonPressed(ORANGE_FRET))
66
Serial.println("Orange Fret Pressed");
67
68
69
if (ps2x.ButtonPressed(STAR_POWER))
70
Serial.println("Star Power Command");
71
72
if (ps2x.Button(UP_STRUM))
73
Serial.println("Up Strum");
74
if (ps2x.Button(DOWN_STRUM))
75
Serial.println("DOWN Strum");
76
77
78
if (ps2x.Button(PSB_START))
79
Serial.println("Start is being held");
80
if (ps2x.Button(PSB_SELECT))
81
Serial.println("Select is being held");
82
83
84
if (ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
85
{
86
Serial.print("Wammy Bar Position:");
87
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
88
}
89
}
90
91
else {
92
ps2x.read_gamepad(false, vibrate);
93
if (ps2x.Button(PSB_START))
94
Serial.println("Start is being held");
95
if (ps2x.Button(PSB_SELECT))
96
Serial.println("Select is being held");
97
if (ps2x.Button(PSB_PAD_UP)) {
98
Serial.print("Up held this hard: ");
99
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
100
}
101
if (ps2x.Button(PSB_PAD_RIGHT)) {
102
Serial.print("Right held this hard: ");
103
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
104
}
105
if (ps2x.Button(PSB_PAD_LEFT)) {
106
Serial.print("LEFT held this hard: ");
107
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
108
}
109
if (ps2x.Button(PSB_PAD_DOWN)) {
110
Serial.print("DOWN held this hard: ");
111
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
112
}
113
vibrate = ps2x.Analog(PSAB_BLUE);
114
115
if (ps2x.NewButtonState())
116
{
117
if (ps2x.Button(PSB_L3))
118
Serial.println("L3 pressed");
119
if (ps2x.Button(PSB_R3))
120
Serial.println("R3 pressed");
121
if (ps2x.Button(PSB_L2))
122
Serial.println("L2 pressed");
123
if (ps2x.Button(PSB_R2))
124
Serial.println("R2 pressed");
125
if (ps2x.Button(PSB_GREEN))
126
Serial.println("Triangle pressed");
127
}
128
129
if (ps2x.ButtonPressed(PSB_RED))
130
Serial.println("Circle just pressed");
131
if (ps2x.ButtonReleased(PSB_PINK))
132
Serial.println("Square just released");
133
if (ps2x.NewButtonState(PSB_BLUE))
134
Serial.println("X just changed");
135
if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1))
136
{
137
Serial.print("Stick Values:");
138
Serial.print(ps2x.Analog(PSS_LY), DEC);
139
Serial.print(",");
140
Serial.print(ps2x.Analog(PSS_LX), DEC);
141
Serial.print(",");
142
Serial.print(ps2x.Analog(PSS_RY), DEC);
143
Serial.print(",");
144
Serial.println(ps2x.Analog(PSS_RX), DEC);
145
}
146
}
147
delay(50);
148
}