PS2手柄按键测试

PS2手柄按键测试arduino的PS2手柄控制测试:#include<PS2X_lib.h>//电机引脚#definePWMA12//A电机转速#defineDIRA134#defineDIRA235//A电机方向#definePWMB8//B电机转速#defineDIRB137#defineDIRB236//B电机方向#def…

大家好,又见面了,我是你们的朋友全栈君。

arduino的PS2手柄控制测试

 #include <PS2X_lib.h>
//电机引脚
#define PWMA 12 //A电机转速
#define DIRA1 34 
#define DIRA2 35 //A电机方向
#define PWMB 8 //B电机转速
#define DIRB1 37 
#define DIRB2 36 //B电机方向
#define PWMC 9 //C电机转速
#define DIRC1 43 
#define DIRC2 42 //C电机方向
#define PWMD 5 //D电机转速
#define DIRD1 26 
#define DIRD2 27 //D电机方向

//PS2控制器
#define PS2_DAT 52 //14 
#define PS2_CMD 51 //15
#define PS2_SEL 53 //16
#define PS2_CLK 50 //17

PS2X ps2x; // create PS2 Controller Class


#define MOTORA_FORWARD(pwm) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,HIGH);analogWrite(PWMA,pwm);}while(0)
#define MOTORA_STOP(x) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,LOW); analogWrite(PWMA,0);}while(0)
#define MOTORA_BACKOFF(pwm) do{digitalWrite(DIRA1,HIGH);digitalWrite(DIRA2,LOW); analogWrite(PWMA,pwm);}while(0)

#define MOTORB_FORWARD(pwm) do{digitalWrite(DIRB1,LOW); digitalWrite(DIRB2,HIGH);analogWrite(PWMB,pwm);}while(0)
#define MOTORB_STOP(x) do{digitalWrite(DIRB1,LOW); digitalWrite(DIRB2,LOW); analogWrite(PWMB,0);}while(0)
#define MOTORB_BACKOFF(pwm) do{digitalWrite(DIRB1,HIGH);digitalWrite(DIRB2,LOW); analogWrite(PWMB,pwm);}while(0)

#define MOTORC_FORWARD(pwm) do{digitalWrite(DIRC1,LOW); digitalWrite(DIRC2,HIGH);analogWrite(PWMC,pwm);}while(0)
#define MOTORC_STOP(x) do{digitalWrite(DIRC1,LOW); digitalWrite(DIRC2,LOW); analogWrite(PWMC,0);}while(0)
#define MOTORC_BACKOFF(pwm) do{digitalWrite(DIRC1,HIGH);digitalWrite(DIRC2,LOW); analogWrite(PWMC,pwm);}while(0)

#define MOTORD_FORWARD(pwm) do{digitalWrite(DIRD1,LOW); digitalWrite(DIRD2,HIGH);analogWrite(PWMD,pwm);}while(0)
#define MOTORD_STOP(x) do{digitalWrite(DIRD1,LOW); digitalWrite(DIRD2,LOW); analogWrite(PWMD,0);}while(0)
#define MOTORD_BACKOFF(pwm) do{digitalWrite(DIRD1,HIGH);digitalWrite(DIRD2,LOW); analogWrite(PWMD,pwm);}while(0)

#define SERIAL Serial

#define MAX_PWM 200
#define MIN_PWM 100
int Motor_PWM = 130;

//控制电机运动 宏定义

// ↑A-----B↑ 
// | ↑ |
// | | |
// ↑C-----D↑
void ADVANCE(){ 
   
  MOTORA_FORWARD(Motor_PWM);MOTORB_FORWARD(Motor_PWM);    
  MOTORC_FORWARD(Motor_PWM);MOTORD_FORWARD(Motor_PWM);    
}

// ↓A-----B↓ 
// | | |
// | ↓ |
// ↓C-----D↓
void BACK(){ 
   
  MOTORA_BACKOFF(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
  MOTORC_BACKOFF(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}

// =A-----B↑ 
// | ↖ |
// | ↖ |
// ↑C-----D=
void LEFT_1(){ 
   
  MOTORA_FORWARD(Motor_PWM);MOTORB_STOP(Motor_PWM);
  MOTORC_STOP(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}

// ↓A-----B↑ 
// | ← |
// | ← |
// ↑C-----D↓
void LEFT_2(){ 
   
  MOTORA_BACKOFF(Motor_PWM);MOTORB_FORWARD(Motor_PWM);
  MOTORC_FORWARD(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}

// ↓A-----B= 
// | ↙ |
// | ↙ |
// =C-----D↓
void LEFT_3(){ 
   
  MOTORA_BACKOFF(Motor_PWM);MOTORB_STOP(Motor_PWM);
  MOTORC_STOP(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}

// ↑A-----B= 
// | ↗ |
// | ↗ |
// =C-----D↑
void RIGHT_1(){ 
   
  MOTORA_FORWARD(Motor_PWM);MOTORB_STOP(Motor_PWM);
  MOTORC_STOP(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}

// ↑A-----B↓ 
// | → |
// | → |
// ↓C-----D↑
void RIGHT_2(){ 
   
  MOTORA_FORWARD(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
  MOTORC_BACKOFF(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}

// =A-----B↓ 
// | ↘ |
// | ↘ |
// ↓C-----D=
void RIGHT_3(){ 
   
  MOTORA_STOP(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
  MOTORC_BACKOFF(Motor_PWM);MOTORD_STOP(Motor_PWM);
}

// =A-----B= 
// | = |
// | = |
// =C-----D=
void STOP(){ 
   
  MOTORA_STOP(Motor_PWM);MOTORB_STOP(Motor_PWM);
  MOTORC_STOP(Motor_PWM);MOTORD_STOP(Motor_PWM);
}

void UART_Control(){ 
   
  char Uart_Date=0;
 if(SERIAL.available())
  { 
   
   Uart_Date = SERIAL.read();
  }
  switch(Uart_Date)
  { 
   
     case 'A':  ADVANCE();              break;
     case 'B':  RIGHT_1();              break;
     case 'C':  RIGHT_2();              break;
     case 'D':  RIGHT_3();              break;
     case 'E':  BACK();                 break;
     case 'F':  LEFT_3();               break;
     case 'G':  LEFT_2();               break;
     case 'H':  LEFT_1();               break;
     case 'Z':  STOP();                 break;
     case 'L':  Motor_PWM = 240;        break;
     case 'M':  Motor_PWM = 130;        break;
   }
}

void IO_init(){ 
   
  pinMode(PWMA, OUTPUT);
  pinMode(DIRA1, OUTPUT);
  pinMode(DIRA2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRB1, OUTPUT);
  pinMode(DIRB2, OUTPUT);
  pinMode(PWMC, OUTPUT);
  pinMode(DIRC1, OUTPUT);
  pinMode(DIRC2, OUTPUT);
  pinMode(PWMD, OUTPUT);
  pinMode(DIRD1, OUTPUT);
  pinMode(DIRD2, OUTPUT);
  STOP();
}

void setup(){ 
   
  int error,type;
  Serial.begin(9600);
  delay(300) ;//added delay to give wireless ps2 module some time to startup, before configuring it
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);

  type = ps2x.readType();
  switch (type) { 
   
  case 0:
    Serial.print("Unknown Controller type found ");
    break;
  case 1:
    Serial.print("DualShock Controller found ");
    break;
  case 2:
    Serial.print("GuitarHero Controller found ");
    break;
  case 3:
    Serial.print("Wireless Sony DualShock Controller found ");
    break;
  }
  IO_init();
}

void loop() { 
   
  byte vibrate = 0;
  UART_Control();//串口接收处理 
  ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
//start 开始运行,电机初PWM为120;
    if (ps2x.Button(PSB_START))  { 
   
      Serial.println("Start");
     Motor_PWM = 90;
      ADVANCE();
    }
// 电机正转;
    if (ps2x.Button(PSB_PAD_UP)) { 
   
      Serial.println("Up held this hard: ");
      Motor_PWM = 120;
     ADVANCE();
    }

// 电机反转;
    if (ps2x.Button(PSB_PAD_DOWN)) { 
   
      Serial.println("Down held this hard: ");
       Motor_PWM = 120;
      BACK();
    }

//左转;
    if (ps2x.Button(PSB_PAD_LEFT)) { 
   
      Serial.println("turn left ");
        Motor_PWM = 120;//200
      LEFT_1();
    }

//右转;
    if (ps2x.Button(PSB_PAD_RIGHT)) { 
   
      Serial.println("turn right");
        Motor_PWM = 120;//200
      RIGHT_1();
    }
// Stop
    if (ps2x.Button(PSB_SELECT)) { 
   
      Serial.println("stop");
      STOP();
    }
// 左平移
    if (ps2x.Button(PSB_PINK)) { 
   
      Serial.println("motor_pmove_left");
      LEFT_2();
    }
// 右平移
    if (ps2x.Button(PSB_RED)) { 
   
      Serial.println("motor_pmove_right");
      RIGHT_2();
    }

    vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
    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_TRIANGLE))
        Serial.println("Triangle pressed");        
    }

    // 摇杆测试
    int LX,LY,RX,RY;
    LY=ps2x.Analog(PSS_LY);  //中间值127
    LX=ps2x.Analog(PSS_LX);  //中间值128
    RY=ps2x.Analog(PSS_RY);  //中间值127
    RX=ps2x.Analog(PSS_RX);  //中间值128
    Serial.print("Stick Values:");
    Serial.print(LY);
    Serial.print(LX);
    Serial.print(RY);
    Serial.println(RX);
    delay(20);
}

ps

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130636.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • How to pause the game in Uniy3D

    How to pause the game in Uniy3D

  • Thread.setDaemon详解

    Thread.setDaemon详解java用户线程与守护线程详解

    2022年10月27日
  • JWT单点登录 看这一篇就够了!

    JWT单点登录 看这一篇就够了!JWT单点登录前言两种登录状态有状态登录无状态登录无状态登录流程整个登录过程中,最关键的点是什么?jwt实现无状态登录数据格式JWT交互流程非对称加密代码实现核心依赖核心工具类JWT工具类RSA工具类测试前言本编在某度上看到了很多关于JWT的实现,但是由于里面讲的太过详细和繁琐,所以打算自己出一篇,有什么不足之处,还希望多多提出来,觉得好的可以点个关注,不喜勿喷,祝猿们天天开心O(∩_∩)O~两种登录状态有状态登录为了保证客户端cookie的安全性,服务端需要记录每次会话的客户端信息,从而识别客户

  • linux chmod 755的含义

    linux chmod 755的含义chmod是Linux下设置文件权限的命令,后面的数字表示不同用户或用户组的权限。一般是三个数字:第一个数字表示文件所有者的权限第二个数字表示与文件所有者同属一个用户组的其他用户的权限第三个数字表示其它用户组的权限。 权限分为三种:读(r=4),写(w=2),执行(x=1) 。 综合起来还有可读可执行(rx=5=4+1)、可读可写(rw=6=4+2)、可读可写可执行(r

  • 下载scrapy失败_手机安装包无法安装怎么办

    下载scrapy失败_手机安装包无法安装怎么办Scrapy安装有问题的,按照这个路径配置下anaconda的环境变量然后进入pycharm里的工作目录输入pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplescrapy安装完成后在cmd中输入scrapy,显示下图内容则证明安装成功:…

  • flask中jsonify和json.dumps的区别「建议收藏」

    flask中jsonify和json.dumps的区别「建议收藏」flask提供了jsonify函数供用户处理返回的序列化json数据,而python自带的json库中也有dumps方法可以序列化json对象,那么在flask的视图函数中return它们会有什么不同之处呢?想必开始很多人和我一样搞不清楚,只知道既然框架提供了方法就用,肯定不会错。但作为开发人员,我们需要弄清楚开发过程中各种实现方式的特点和区别,这样在我们面对不同的需求时才能做出相对合理的选择,而…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号