2.2轻松入门-拓展PS2手柄控制例程

接线:

Mixly轻松入门-拓展:PS2手柄控制例程
Mixly轻松入门-拓展:PS2手柄控制例程

效果:

图形编程:

Mixly轻松入门-拓展:PS2手柄控制例程

代码:

#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);

}

文件下载

联系我们

联系我们

微信:13823392571

在线咨询:点击这里给我发消息

工作时间:周一至周五,9:30-18:30,节假日休息

微信客服
微信客服
分享本页
返回顶部