Mixly轻松入门-拓展:PS2手柄控制例程
arduino •
接线:
效果:
图形编程:
代码:
#define PS2_DAT 12
#define PS2_CMD 11
#define PS2_CS 10
#define PS2_CLK 13
#include "PS2X_lib.h"
volatile int flag_rocker;
volatile int flag_motor1;
PS2X ps2x;
void setup(){
flag_rocker = 0;
flag_motor1 = 0;
Serial.begin(9600);
ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_CS, PS2_DAT, true, true);
}
void loop(){
if (flag_motor1 == 0) {
ps2x.read_gamepad(false,ps2x.Analog(PSAB_CROSS));
} else {
ps2x.read_gamepad(true,0);
}
if (ps2x.ButtonPressed(PSB_START)) {
Serial.println("Start pressed");
}
if (ps2x.ButtonPressed(PSB_SELECT)) {
Serial.println("Select pressed");
}
if (ps2x.ButtonPressed(PSB_PAD_UP)) {
Serial.print("Up held this hard:");
Serial.println(ps2x.Analog(PSAB_PAD_UP));
}
if (ps2x.ButtonPressed(PSB_PAD_RIGHT)) {
Serial.print("Right held this hard:");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT));
}
if (ps2x.ButtonPressed(PSB_PAD_LEFT)) {
Serial.print("Left held this hard:");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT));
}
if (ps2x.ButtonPressed(PSB_PAD_DOWN)) {
Serial.print("Down held this hard:");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN));
flag_motor1 = 1;
} else if (ps2x.ButtonReleased(PSB_PAD_DOWN) == 1) {
flag_motor1 = 0;
}
if (ps2x.ButtonPressed(PSB_CROSS)) {
Serial.println("Cross");
}
if (ps2x.ButtonPressed(PSB_CIRCLE)) {
Serial.println("Circle");
}
if (ps2x.ButtonPressed(PSB_SQUARE)) {
Serial.println("Square");
}
if (ps2x.ButtonPressed(PSB_TRIANGLE)) {
Serial.println("Triangle");
}
if (ps2x.ButtonPressed(PSB_L1)) {
Serial.println("L1");
flag_rocker = 1;
} else if (ps2x.ButtonReleased(PSB_L1) == 1) {
flag_rocker = 0;
}
if (ps2x.ButtonPressed(PSB_R1)) {
Serial.println("R1");
flag_rocker = 1;
} else if (ps2x.ButtonReleased(PSB_R1) == 1) {
flag_rocker = 0;
}
if (ps2x.ButtonPressed(PSB_L2)) {
Serial.println("L2");
}
if (ps2x.ButtonPressed(PSB_R2)) {
Serial.println("R2");
}
if (ps2x.ButtonPressed(PSB_L3)) {
Serial.println("L3");
}
if (ps2x.ButtonPressed(PSB_R3)) {
Serial.println("R3");
}
if (flag_rocker == 1) {
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LX));
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LY));
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RX));
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RY));
}
delay(30);
}
文件下载:https://www.haibucuo.com/download/mixly%e8%bd%bb%e6%9d%be%e5%85%a5%e9%97%a8-%e6%8b%93%e5%b1%95%ef%bc%9aps2%e6%89%8b%e6%9f%84%e6%8e%a7%e5%88%b6%e4%be%8b%e7%a8%8b/embed#?secret=ScFxN301Ll