精华内容
下载资源
问答
  • arduino ps2手柄操作库,用于Arduino开发,机器人小车,网上不是很好找,免费的资源不多,发出来共享一下。
  • Arduino ps2手柄库PS2X_lib.rar
  • ps2手柄arduino库文件

    2018-09-07 20:51:21
    该资源是Arduinops2手柄通信的库文件,想学习SONYps2手柄Arduino 的老铁可以下载安装此库文件,祝各位老铁学习愉快。
  • Arduino PS2手柄小车代码

    千次阅读 2020-11-13 23:12:05
    Arduino PS2手柄小车代码 1、首先需要给Arduino的库文件中加入<PS2X_lib.h>这个库 https://github.com/madsci1016/Arduino-PS2X 2、接好小车左右转的电机 3、还需要将PS2手柄接收器和Arduino的板子接好线 4、...

    Arduino PS2手柄小车代码
    1、首先需要给Arduino的库文件中加入<PS2X_lib.h>这个库
    https://github.com/madsci1016/Arduino-PS2X
    2、接好小车左右转的电机
    3、还需要将PS2手柄接收器和Arduino的板子接好线
    4、烧录好下面的程序 开始感受快乐吧~~~
    在这里插入图片描述

    Alt

    #include <PS2X_lib.h>  
    PS2X ps2x; // create PS2 Controller Class
    int leftmotor1 = 2;
    int leftmotor2 =3;
    int rightmotor1 = 4 ;
    int rightmotor2 = 5 ;
    int EN1=6;
    int EN2=9;
    
    int error = 0; 
    byte type = 0;
    byte vibrate = 0;
    
    void setup(){  
      pinMode (leftmotor1, OUTPUT );
      pinMode  (leftmotor2, OUTPUT);
      pinMode (rightmotor1, OUTPUT );
      pinMode ( rightmotor2, OUTPUT );
         pinMode (EN1, OUTPUT );
      pinMode ( EN2, OUTPUT );
                           
    Serial.begin(57600);
    error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
       type = ps2x.readType(); 
    }
    
    void loop()
    {
       ps2x.read_gamepad();          //read controller 
        ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
          vibrate = ps2x.Analog(PSAB_BLUE); 
     /*   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)) 
         {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())               //改变状态时
        {
             if(ps2x.Button(PSB_L3))
             Serial.println("L3");
            if(ps2x.Button(PSB_R3))
             Serial.println("R3");
            if(ps2x.Button(PSB_L2))
             Serial.println("L2");
            if(ps2x.Button(PSB_R2))
             Serial.println("R2");
            if(ps2x.Button(PSB_GREEN))
             Serial.println("三角形");    
        }   
             
        
        if(ps2x.ButtonPressed(PSB_RED))             //被按
             Serial.println("圆圈");
             
        if(ps2x.ButtonReleased(PSB_PINK))             //被松开
             Serial.println("正方形");     
        
        if(ps2x.NewButtonState(PSB_BLUE))            //被按和被松开
             Serial.println("X");    
    */
    
    if((ps2x.Analog(PSS_LY)>100)&&(ps2x.Analog(PSS_LY)<150))   
          {   digitalWrite ( leftmotor1,LOW );
           digitalWrite ( leftmotor2,LOW );
           digitalWrite ( rightmotor1,LOW );
           digitalWrite ( rightmotor2,LOW );
        }  
    
    
    
         if(ps2x.Analog(PSS_LY)<100)    //qianjin
          {digitalWrite ( leftmotor1,HIGH );
           digitalWrite ( leftmotor2,LOW );
           digitalWrite ( rightmotor1,HIGH );
           digitalWrite ( rightmotor2,LOW );
           analogWrite ( EN1,100);
           analogWrite ( EN2,100);
              }
           if(ps2x.Analog(PSS_LY)>150)    //左摇杆后推   后退
           {digitalWrite ( leftmotor1,LOW );
           digitalWrite ( leftmotor2,HIGH );
           digitalWrite ( rightmotor1,LOW );
           digitalWrite ( rightmotor2,HIGH );
         analogWrite ( EN1,100);
           analogWrite ( EN2,100);
        }   
          if(ps2x.Analog(PSS_RX)<100)      //右摇杆左推   左转
          { digitalWrite ( leftmotor1,LOW );
           digitalWrite ( leftmotor2,HIGH );
           digitalWrite ( rightmotor1,HIGH );
           digitalWrite ( rightmotor2,LOW);
        analogWrite ( EN1,100);
           analogWrite ( EN2,100);
            }
              if(ps2x.Analog(PSS_RX)>150)      //右摇杆右推   右转
          {digitalWrite ( leftmotor1,HIGH );
           digitalWrite ( leftmotor2,LOW );
           digitalWrite ( rightmotor1,LOW);
           digitalWrite ( rightmotor2,HIGH );
            analogWrite ( EN1,100);
           analogWrite ( EN2,100);
        }
    
    
    
    
    
         
        
           
            Serial.print(ps2x.Analog(PSS_LY), DEC); 
            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);
         
    }
    
    展开全文
  • 索尼PS2手柄破解协议例程 arduino ps2遥控小车,舵机,手柄震动.
  • 手柄震动驱动将能够完美的解决手柄驱动问题,拥有方便的驱动支持服务,各类驱动效果堪称完美,能够达到舒适的使用体验,轻松解决手柄驱动的各项难题,让你感受完美的操控体验,尽情的实现最佳的游戏控制效果!...
  • Arduino PS2无线遥控手柄的详细设计资料,包括ps2库,样例代码,还有智能小车源程序等
  • Arduino控制PS2无线手柄

    千次阅读 多人点赞 2019-05-19 22:26:59
    使用的是开源社区贡献的arduino PS2X库市面上也有不少PS2手柄,但是长得和PS2原装手柄有区别,这种手柄可以在PS2主机上使用,不一定可以配合此库使用,这也是因为这个库是逆向破解PS2协议的原因,所以肯定有哪里不太...

    前言

    使用的是开源社区贡献的arduino PS2X库市面上也有不少PS2手柄,但是长得和PS2原装手柄有区别,这种手柄可以在PS2主机上使用,不一定可以配合此库使用,这也是因为这个库是逆向破解PS2协议的原因,所以肯定有哪里不太完美。

    先来看看硬件连接图
    在这里插入图片描述
    在这里插入图片描述

    然后把附件中的PS2X库安装到ArduinoIDE中。点击下载附件 提取码: 51pp 我用的Arduino是1.8的版本

    安装PS2X的教程:

    选择项目–>加载库–>添加ZIP–>选择附件中的ps2x_lib 即可

    此代码是示例:

    #include <PS2X_lib.h>
    
    #include <PS2X_lib.h>  //for v1.6
    
    PS2X ps2x; // create PS2 Controller Class
    
    //right now, the library does NOT support hot pluggable controllers, meaning 
    //you must always either restart your Arduino after you conect the controller, 
    //or call config_gamepad(pins) again after connecting the controller.
    int Left_motor_back=9;       //左电机后退(IN1)
    int Left_motor_go=5;         //左电机前进(IN2)
    
    int Right_motor_go=6;        // 右电机前进(IN3)
    int Right_motor_back=10;    // 右电机后退(IN4)
    
    int Right_motor_en=8;      // 右电机前进(EN2)
    int Left_motor_en=7;      // 右电机后退(EN1)
    
    int error = 0; 
    byte type = 0;
    byte vibrate = 0;
    
    void setup(){
    Serial.begin(57600);
     //初始化电机驱动IO为输出方式
      pinMode(Left_motor_go,OUTPUT); // 
      pinMode(Left_motor_back,OUTPUT); // 
      pinMode(Right_motor_go,OUTPUT);// 
      pinMode(Right_motor_back,OUTPUT);// 
    //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
      
    error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
    
    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 [url]www.billporter.info[/url] for updates and to report bugs.");
    }
       
      else if(error == 1)
       Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit [url]www.billporter.info[/url] for troubleshooting tips");
       
      else if(error == 2)
       Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url]www.billporter.info[/url] for troubleshooting tips");
       
      else if(error == 3)
       Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
       
       //Serial.print(ps2x.Analog(1), HEX);
       
       type = ps2x.readType(); 
         switch(type) {
           case 0:
            Serial.println("Unknown Controller type");
           break;
           case 1:
            Serial.println("DualShock Controller Found");
           break;
           case 2:
             Serial.println("GuitarHero Controller Found");
           break;
         }
      
    }
    
    void loop(){
       /* You must Read Gamepad to get new values
       Read GamePad and set vibration values
       ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
       if you don't enable the rumble, use ps2x.read_gamepad(); with no values
       
       you should call this at least once a second
       */
       
       
       
    if(error == 1) //skip loop if no controller found
      return; 
      
    if(type == 2){ //Guitar Hero Controller
       
       ps2x.read_gamepad();          //read controller 
       
       if(ps2x.ButtonPressed(GREEN_FRET))
         Serial.println("Green Fret Pressed");
       if(ps2x.ButtonPressed(RED_FRET))
         Serial.println("Red Fret Pressed");
       if(ps2x.ButtonPressed(YELLOW_FRET))
         Serial.println("Yellow Fret Pressed");
       if(ps2x.ButtonPressed(BLUE_FRET))
         Serial.println("Blue Fret Pressed");
       if(ps2x.ButtonPressed(ORANGE_FRET))
         Serial.println("Orange Fret Pressed");
         
    
        if(ps2x.ButtonPressed(STAR_POWER))
         Serial.println("Star Power Command");
        
        if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
         Serial.println("Up Strum");
        if(ps2x.Button(DOWN_STRUM))
         Serial.println("DOWN Strum");
      
    
        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(ORANGE_FRET)) // print stick value IF TRUE
        {
            Serial.print("Wammy Bar Position:");
            Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
        } 
    }
    
    else { //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);
          }   
      
        
          vibrate = ps2x.Analog(PSAB_BLUE);        //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_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.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);
         
    }
    

    将示例代码上传到UNO板中

    打开ps2无线手柄的开关 观察无线接收器的绿灯 与手柄的上的灯 都停止闪烁说明连接成功了。

    打开串口监视器
    在这里插入图片描述

    如图就是连接成功的显示

    我们按下手柄的按键 则会显示对应文字

    这样就连接成功了!

    展开全文
  • 基于PS2手柄Arduino遥控小车

    千次阅读 2021-02-21 15:12:23
    本文利用PS2手柄Arduino开发板制作了一个简易的遥控小车,利用蓝牙进行通信,可以实现前后左右的移动。(原理掌握之后可以自己拓展相关功能) 一、零件 1.Arduino UNO开发板: ArduinoUNO是ArduinoUSB接口系列的...

    前言

    本文利用PS2手柄和Arduino开发板制作了一个简易的遥控小车,利用蓝牙进行通信,可以实现前后左右的移动。(原理掌握之后可以自己拓展相关功能)

    一、零件

    1.Arduino UNO开发板:

    ArduinoUNO是ArduinoUSB接口系列的最新版本,作为Arduino平台的参考标准模板。UNO的处理器核心是ATmega328,同时具有14路数字输入/输出口(其中6路可作为PWM输出),6路模拟输入,一个16MHz晶体振荡器,一个USB口,一个电源插座,一个ICSPheader和一个复位按钮。
    在这里插入图片描述

    2.PS2手柄与接收器:

    PS2 手柄由手柄与接收器两部分组成,手柄主要负责发送按键信息;接收器与单片机相连,用于接收手柄发来的信息,并传递给单片机,单片机也可通过接收器,向手柄发送命令,配置手柄的发送模式。
    在这里插入图片描述
    接收器的引脚功能如下图所示:
    在这里插入图片描述
    特别需要提醒的是:按下遥控器的 MODE 键,手柄可配置成“红灯模式”或者“绿灯模式”,当遥控器侧面的 “MODE LED”亮为红灯模式,反之为绿灯模式,“红灯模式”和“绿灯模式”的主要区别是摇杆模拟值得输出。

    3.L298N驱动模块:

    L298N,是一款接受高电压的电机驱动器,直流电机和步进电机都可以驱动。一片驱动芯片可同时控制两个直流减速电机做不同动作,在6V到46V的电压范围内,提供2安培的电流,并且具有过热自断和反馈检测功能。L298N可对电机进行直接控制,通过主控芯片的I/O输入对其控制电平进行设定,就可为电机进行正转反转驱动,操作简单、稳定性好,可以满足直流电机的大电流驱动条件。

    在这里插入图片描述

    4.面包板

    面包板是由于板子上有很多小插孔,专为电子电路的无焊接实验设计制造的。由于各种电子元器件可根据需要随意插入或拔出,免去了焊接,节省了电路的组装时间,而且元件可以重复使用,所以非常适合电子电路的组装、调试和训练。

    在这里插入图片描述

    5.小车

    由亚克力板、直流电机、小轮胎、电池盒、电池等搭建的小车。
    在这里插入图片描述

    二、代码

    #include <PS2X_lib.h>  //for v1.6
    
    #define PS2_DAT        13  //14    
    #define PS2_CMD        11  //15
    #define PS2_SEL        10  //16
    #define PS2_CLK        12  //17
    
    int input1 = 5; //PIN 5(PWM)
    int input2 = 6; //PIN 6(PWM)
    int input3 = 9; //PIN 9(PWM)
    int input4 = 3; //PIN 3(PWM)
    
    #define pressures   true
    #define rumble      true
    
    PS2X ps2x; // create PS2 Controller Class
    
    int error = 0;
    byte type = 0;
    byte vibrate = 0;
    
    // Reset func 
    void (* resetFunc) (void) = 0;
    
    void setup(){
     
      Serial.begin(9600);
      
      delay(500);  //added delay to give wireless ps2 module some time to startup, before configuring it
       
      error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
      
      if(error == 0){
        Serial.print("Found Controller, configured successful ");
        Serial.print("pressures = ");
    	if (pressures)
    	  Serial.println("true ");
    	else
    	  Serial.println("false");
    	Serial.print("rumble = ");
    	if (rumble)
    	  Serial.println("true)");
    	else
    	  Serial.println("false");
        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("Note: Go to www.billporter.info for updates and to report bugs.");
      }  
      else if(error == 1)
        Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
       
      else if(error == 2)
        Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
    
      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("Unknown Controller type found ");
          break;
        case 1:
          Serial.println("DualShock Controller found ");
          break;
        case 2:
          Serial.println("GuitarHero Controller found ");
          break;
    	case 3:
          Serial.println("Wireless Sony DualShock Controller found ");
          break;
       }
    pinMode(input1,OUTPUT);
    pinMode(input2,OUTPUT);                                                    
    pinMode(input3,OUTPUT); 
    pinMode(input4,OUTPUT);
    }
    
    
    void STOP(){
     digitalWrite(input1,LOW);
     digitalWrite(input2,LOW);  
     digitalWrite(input3,LOW);
     digitalWrite(input4,LOW);  
    }//停止函数
    
    void RUN(){
      digitalWrite(input1,HIGH); 
      digitalWrite(input2,LOW); 
      digitalWrite(input3,LOW);
      digitalWrite(input4,HIGH); 
     analogWrite(input1,250);
     analogWrite(input4,250);
    }//可调速的前进函数
    
    void LEFT(){
      digitalWrite(input1,HIGH); 
      digitalWrite(input2,LOW);  
      digitalWrite(input3,HIGH); 
      digitalWrite(input4,LOW); 
     analogWrite(input1,200);
     analogWrite(input3,200);
    }//可调速的左转函数
    
    void RIGHT(){
      digitalWrite(input1,LOW);
      digitalWrite(input2,HIGH);  
      digitalWrite(input3,LOW);
      digitalWrite(input4,HIGH); 
     analogWrite(input2,200);
     analogWrite(input4,200);
    }//可调速的右转函数
    
    void BACK(){
      digitalWrite(input1,LOW); 
      digitalWrite(input2,HIGH); 
      digitalWrite(input3,HIGH);
      digitalWrite(input4,LOW); 
     analogWrite(input2,250);
     analogWrite(input3,250);
    }//可调速的后退函数
    void loop() {
      
      if(error == 1){ //skip loop if no controller found
        resetFunc();
      }
      
      if(type == 2){ //Guitar Hero Controller
        ps2x.read_gamepad();          //read controller 
       
        if(ps2x.ButtonPressed(GREEN_FRET))
          Serial.println("Green Fret Pressed");
        if(ps2x.ButtonPressed(RED_FRET))
          Serial.println("Red Fret Pressed");
        if(ps2x.ButtonPressed(YELLOW_FRET))
          Serial.println("Yellow Fret Pressed");
        if(ps2x.ButtonPressed(BLUE_FRET))
          Serial.println("Blue Fret Pressed");
        if(ps2x.ButtonPressed(ORANGE_FRET))
          Serial.println("Orange Fret Pressed"); 
    
        if(ps2x.ButtonPressed(STAR_POWER))
          Serial.println("Star Power Command");
        
        if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
          Serial.println("Up Strum");
        if(ps2x.Button(DOWN_STRUM))
          Serial.println("DOWN Strum");
     
        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(ORANGE_FRET)) {     // print stick value IF TRUE
          Serial.print("Wammy Bar Position:");
          Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
        } 
      }
      else { //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: ");
          RUN();
          Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
        }
        if(ps2x.Button(PSB_PAD_RIGHT)){
          Serial.print("Right held this hard: ");
          LEFT();
          Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
        }
        if(ps2x.Button(PSB_PAD_LEFT)){
          Serial.print("LEFT held this hard: ");
          RIGHT();
          Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
        }
        if(ps2x.Button(PSB_PAD_DOWN)){
          Serial.print("DOWN held this hard: ");
          BACK();
          Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
        }   
    
        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");   
            STOP();      
        }
    
        if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
          Serial.println("Circle just pressed");
        if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
          Serial.println("X just changed");
        if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
          Serial.println("Square just released");     
    
        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);  
    }
    
    展开全文
  • Arduino-PS2手柄汉化注释程序文件,网上大都是全英文的,这边汉化注释及串口通讯信息便于理解和移植。
  • 1.ps2无线手柄,某宝购入 2.arduino UNO板,某宝购入 3.杜邦线若干,某宝购入 接线 颜色 插口 蓝 13 绿 10 红 3.3v 黑 GND 黄 11 橙 12 引入PS2X库 库下载链接: ...

    试验器材

    1.ps2无线手柄,某宝购入(建议买带插针的,直接接的貌似干扰比较强)
    2.arduino UNO板,某宝购入
    3.杜邦线若干,某宝购入

    接线

    如图

    颜色插口
    蓝(Clock)13
    绿(Attention)10
    红(Power)3.3v
    黑(GND)GND
    黄(Command)11
    橙(Data)12

    引入PS2X库

    库下载链接: https://github.com/madsci1016/Arduino-PS2X.

    引入方法

    库文件目录直接拷贝到arduino IDE的libraries文件夹下

    在这里插入图片描述
    重启arduino IDE之后可以看到:
    在这里插入图片描述
    参考代码
    感谢:参考链接

    #include <PS2X_lib.h>   
    
    PS2X ps2x;
    
    //right now, the library does NOT support hot-pluggable controllers, meaning
    //you must always either restart your Arduino after you connect the controller,
    //or call config_gamepad(pins) again after connecting the controller.
    
    int error = 0;
    byte type = 0;
    byte vibrate = 0;
    
    void setup(){
    Serial.begin(57600);
    
      
    error = ps2x.config_gamepad(13,11,10,12, true, true);   //GamePad(clock, command, attention, data, Pressures?, Rumble?)
    
    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, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
       
      else if(error == 2)
       Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
       
      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("Unknown Controller type");
           break;
           case 1:
            Serial.println("DualShock Controller Found");
           break;
           case 2:
             Serial.println("GuitarHero Controller Found");
           break;
         }
      
    }
    
    void loop(){
       /* You must Read Gamepad to get new values
       Read GamePad and set vibration values
       ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
       if you don't enable the rumble, use ps2x.read_gamepad(); with no values
       
       you should call this at least once a second
       */
       
    if(error == 1)
      return;
      
    if(type == 2){
       
       ps2x.read_gamepad();          //read controller
       
       if(ps2x.ButtonPressed(GREEN_FRET))
         Serial.println("Green Fret Pressed");
       if(ps2x.ButtonPressed(RED_FRET))
         Serial.println("Red Fret Pressed");
       if(ps2x.ButtonPressed(YELLOW_FRET))
         Serial.println("Yellow Fret Pressed");
       if(ps2x.ButtonPressed(BLUE_FRET))
         Serial.println("Blue Fret Pressed");
       if(ps2x.ButtonPressed(ORANGE_FRET))
         Serial.println("Orange Fret Pressed");
         
    
        if(ps2x.ButtonPressed(STAR_POWER))
         Serial.println("Star Power Command");
       
        if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
         Serial.println("Up Strum");
        if(ps2x.Button(DOWN_STRUM))
         Serial.println("DOWN Strum");
      
    
        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(ORANGE_FRET)) // print stick value IF TRUE
        {
            Serial.print("Wammy Bar Position:");
            Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
        }
    }
    
    else { //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);
          }   
      
       
          vibrate = ps2x.Analog(PSAB_BLUE);        //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_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.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);
         
    }
    

    注意事项:

    1. PS2手柄需要与接收器配对,按MODE键配对。
    2. PS2手柄配对超时灯会熄灭,按手柄上的START键重新配对
    3. 杜邦线接PS2接收器针脚时如果不好接线可以把插头拔掉直接接入
      如图:在这里插入图片描述

    成果展示:

    arduino IDE自带串口工具查看串口输出:
    手柄检测
    按键测试
    续集链接-https://blog.csdn.net/qq_30019617/article/details/109444260
    续集里重新买了一个ps2手柄和接收器,带插针的。感觉要稳定一些。之前的一直有干扰。不是很灵敏

    展开全文
  • ps2无线手柄连接arduino的使用方法

    千次阅读 2019-07-20 15:14:24
    首先在https://github.com/madsci1016/Arduino-PS2X这里下载ps2x的库文件。 将PS2X_lib放到库文件目录下。 接线方法如下 , 代码部分 #define pressures true 的意思是让上下左右的按键可以度数,按下时...
  • PS2手柄遥控控制灯开关(arduino

    千次阅读 2019-10-16 22:24:05
    PS2手柄遥控控制灯开关(arduino) 使用的材料 arduino板子: ps2遥控手柄; 一个5V供电的LDE; 连线 首先按照遥控手柄底座上的英文,你会发现一共有六根线,分别是GND,VCC,DAT,CMD,CS,CL。GND连接arduino板子上的...
  • 一、PS2接线说明 功能: 1,数据:控制器- > PlayStation 游戏机。这是一个集电极开路输出,需要一个上拉电阻(10K,或许更多)。(上拉电阻是必要的,因为控制器只能连接线对地,它不能真正把电压就行了)。 2...
  • 前篇链接-arduino连接ps2手柄控制智能小车实践记录 本篇主要任务: 接电机 根据控制调整电机旋转方向 解决初始化时手柄检测问题(每次启动未必能找到手柄) 任务一:接电机 电机控制芯片:L298N(淘宝购) 电机...
  • 使用Arduino mega 2560读取PS2无线手柄信号并控制小车运动 文章目录使用Arduino mega 2560读取PS2无线手柄信号并控制小车运动1 前言2 硬件连接2.1 硬件构成简介2.2 硬件连线3 程序编制4 实际测试5 TIPS 1 前言 最近...
  • PS2手柄 通信 协议解析..............................................................................................................................
  • PS2游戏手柄文章 http://www.billporter.info/?p=240 http://www.billporter.info/2010/06/05/playstation-2-controller-arduino-library-v1-0/ https://blog.csdn.net/weixin_43336281/article/details/88628049 ...
  • PS2无线手柄控制智能小车的Mixly图形编程程序,程序使用Arduino uno单片机主板,用Mixly1.1.7编译新版本自动加了YFRobot-v222库文件。
  • PS2+ arduino+接线图+源码
  • 机器人擂台赛开发程序,专用程序,经过多年精心改造的,遥控机器人专用程序
  • 目录前言1,关于arduino 控制PS2手柄2,接线配置3,总结 前言 相关arduino 全部分类: https://blog.csdn.net/freewebsys/category_8799254.html 本文的原文连接是: ...未经博主允许不得转载。 博主地址是:htt...
  • PS2手柄遥控Arduino小车

    万次阅读 多人点赞 2017-08-17 13:53:53
    使用手柄遥控小车是经常要用到的,看到PS2手柄很6,就拿来尝试一下。 PS2手柄是索尼的PlayStation2游戏机的遥控手柄,因为这款手柄性价比较高,按键丰富,方便扩展到其它应用中,后来有人将其通讯协议破解,使得...
  • arduino学习笔记(8)-- 读取ps2摇杆值

    千次阅读 2019-03-13 15:05:07
    arduino读取ps2摇杆值 ps2摇杆可以任意方向操作,原理是2个可变电位器组成,任意方向分别用X和Y轴表示。 使用arduino的读取摇杆模块的2个模拟输出值,需要注意的是,从模拟口uno的A0-A5读取的模拟值为0-1023,但是...
  • Arduino简单实例之四_PS2游戏摇杆

    万次阅读 2017-02-20 13:08:10
    1) 说明: PS2 游戏双轴摇杆传感器模块由采用金属 PS2 摇杆电位器制作,具有(X,Y)2 轴模拟输出,(Z) 1路按钮数字输出。...PS2游戏摇杆joystick,Arduino uno,杜邦线  3) 连接: 5V接 Arduino 5V GND接 Ardu
  • MJKDZ PS2手柄控制OskarBot小车(一):Arduino串口发送数据 【目录】  - 1、无线通信模块设置  - 1.1 设置参数  - 1.2 调试步骤  - 2、按键与通信格式  - 2.1 PS2按键定义  - 2.2 发送数据格式  - ...
  • PS2手柄按键测试

    万次阅读 2019-12-08 18:23:18
    arduinoPS2手柄控制测试: #include <PS2X_lib.h> //电机引脚 #define PWMA 12 //A电机转速 #define DIRA1 34 #define DIRA2 35 //A电机方向 #define PWMB 8 //B电机转速 #define DIRB1 37 #...
  • 目录前言1,关于arduino PS2手柄2,设备接入3,Arduino代码3,总结 前言 相关arduino 全部分类: https://blog.csdn.net/freewebsys/category_8799254.html 本文的原文连接是: ...未经博主允许不得转载。...

空空如也

空空如也

1 2 3 4 5 6
收藏数 111
精华内容 44
关键字:

arduinops2手柄