找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4939|回复: 1
打印 上一主题 下一主题
收起左侧

ps2手柄的Arduino源码,所有的运行程序与资料

[复制链接]
跳转到指定楼层
楼主
ps2手柄,所有的运行程序,arduino资料


Arduino源程序如下:

  1. //#include <Servo.h>
  2. #include <PS2X_lib.h>  //for v1.6
  3. PS2X ps2x;
  4. //Servo myservo;

  5. #define PS2_DAT       13  //14   
  6. #define PS2_CMD        7  //15
  7. #define PS2_SEL        8  //16
  8. #define PS2_CLK        12  //17
  9. #define PWMA1           3  //Motor A's pin
  10. #define PWMA2           5 //Motor A's pin
  11. #define PWMA3           9  //Motor A's pin
  12. #define PWMA4           6 //Motor A's pin
  13. #define SERVOPIN       4   //servo's pin

  14. #define SERVOMIN 30      //the angle of servo
  15. #define SERVOMID 90
  16. #define SERVOMAX 150
  17. #define TURNLEFT myservo.write(SERVOMIN)  //turn left
  18. #define TURNMID myservo.write(SERVOMID)   //turn middle
  19. #define TURNRIGHT myservo.write(SERVOMAX)  //turn right
  20. #define MOTORFOWARD setMotorA(255)    //motor foward
  21. #define MOTORBACK setMotorA(-255)     //motor back
  22. #define MOTORSTOP setMotorA(0)        //motor stop

  23. bool EnableRockerR = 1;
  24. bool EnableRockerL = 1;
  25. int RMid = 0 ;
  26. int LStop = 0 ;

  27. #define pressures   false
  28. #define rumble      false
  29. int error = 0;
  30. byte type = 0;
  31. byte vibrate = 0;

  32. void setup() {
  33.   Serial.begin(57600);
  34.   pinMode(PWMA1, OUTPUT);
  35.   pinMode(PWMA2, OUTPUT);
  36.   pinMode(PWMA3, OUTPUT);
  37.   pinMode(PWMA4, OUTPUT);
  38.   myservo.attach(SERVOPIN);
  39.   SERVOMID;
  40.   delay(300);
  41.   error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

  42.   if (error == 0) {
  43.     Serial.print("Found Controller, configured successful ");
  44.     Serial.print("pressures = ");
  45.     if (pressures)
  46.       Serial.println("true ");
  47.     else
  48.       Serial.println("false");
  49.     Serial.print("rumble = ");
  50.     if (rumble)
  51.       Serial.println("true)");
  52.     else
  53.       Serial.println("false");
  54.     Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  55.     Serial.println("holding L1 or R1 will print out the analog stick values.");
  56.     Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  57.   }
  58.   else if (error == 1)
  59.     Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");

  60.   else if (error == 2)
  61.     Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  62.   else if (error == 3)
  63.     Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

  64.   type = ps2x.readType();
  65.   switch (type) {
  66.     case 0:
  67.       Serial.print("Unknown Controller type found ");
  68.       break;
  69.     case 1:
  70.       Serial.print("DualShock Controller found ");
  71.       break;
  72.     case 2:
  73.       Serial.print("GuitarHero Controller found ");
  74.       break;
  75.     case 3:
  76.       Serial.print("Wireless Sony DualShock Controller found ");
  77.       break;
  78.   }
  79. }
  80. void(* resetFunc) (void) = 0;
  81. void loop() {
  82.   if (error == 1) //skip loop if no controller found
  83.     resetFunc();
  84.   ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed

  85.   if (ps2x.Button(PSB_START))        //will be TRUE as long as button is pressed
  86.     Serial.println("Start is being held");
  87.   if (ps2x.Button(PSB_SELECT))
  88.     Serial.println("Select is being held");


  89.   if (ps2x.ButtonPressed(PSB_PAD_UP)) {
  90.     Serial.println("PAD_UP just pressed");
  91.     MOTORFOWARD;
  92.     EnableRockerL = 0;
  93.   }else if(ps2x.ButtonReleased(PSB_PAD_UP)) {
  94.     Serial.println(" PAD_UP just released");
  95.     MOTORSTOP;
  96.     EnableRockerL = 1;
  97.     LStop = 0;
  98.   }

  99.   if (ps2x.ButtonPressed(PSB_PAD_DOWN)) {
  100.     Serial.println("PAD_DOWN just pressed");
  101.     MOTORBACK;
  102.     EnableRockerL = 0;
  103.   }else if(ps2x.ButtonReleased(PSB_PAD_DOWN)) {
  104.     Serial.println(" PAD_DOWN just released");
  105.     MOTORSTOP;
  106.     EnableRockerL = 1;
  107.     LStop = 0;
  108.   }

  109.   if (EnableRockerL) {
  110.     if (0 <= ps2x.Analog(PSS_LY) && ps2x.Analog(PSS_LY) < 100) {
  111.       Serial.println(" f");
  112.       MOTORFOWARD;
  113.       LStop = 0;
  114.     } else if (255 >= ps2x.Analog(PSS_LY) && ps2x.Analog(PSS_LY) > 170) {
  115.       MOTORBACK;
  116.       Serial.println(" b");
  117.       LStop = 0;
  118.     } else {
  119.       if(LStop == 0){
  120.         MOTORSTOP;
  121.         Serial.println(" s");
  122.         LStop ++;
  123.       }
  124.     }
  125.   }


  126.   if (ps2x.ButtonPressed(PSB_CIRCLE)) {             //will be TRUE if button was JUST pressed
  127.     Serial.println("Circle just pressed");
  128.     TURNRIGHT;
  129.     EnableRockerR = 0;
  130.   } else if (ps2x.ButtonReleased(PSB_CIRCLE)) {
  131.     Serial.println("Circle just released");
  132.     TURNMID;
  133.     EnableRockerR = 1;
  134.     RMid = 0;
  135.   }
  136.   
  137.   if (ps2x.ButtonPressed(PSB_SQUARE)) {            //will be TRUE if button was JUST released
  138.     Serial.println("Square just pressed");
  139.     TURNLEFT;
  140.     EnableRockerR = 0;
  141.   } else if (ps2x.ButtonReleased(PSB_SQUARE)) {            //will be TRUE if button was JUST released
  142.     Serial.println("Square just released");
  143.     TURNMID;
  144.     EnableRockerR = 1;
  145.     RMid = 0;
  146.   }

  147.   if (EnableRockerR) {
  148.     if (0 <= ps2x.Analog(PSS_RX)&ps2x.Analog(PSS_RX) < 100) {
  149.       TURNLEFT;
  150.       Serial.println(" l");
  151.       RMid = 0;
  152.     } else if (255 >= ps2x.Analog(PSS_RX)&ps2x.Analog(PSS_RX) > 160) {
  153.       TURNRIGHT;
  154.       Serial.println(" r");
  155.       RMid = 0;
  156.     } else {
  157.       if(RMid == 0){
  158.         TURNMID;
  159.         Serial.println(" mid");
  160.         RMid ++;
  161.       }
  162.     }
  163.   }


  164.   if (ps2x.NewButtonState(PSB_CROSS)) {             //will be TRUE if button was JUST pressed OR released
  165.     Serial.println("X just changed");
  166.     MOTORSTOP;
  167.   }
  168.   delay(50);
  169. }



  170. void setMotorA(int m1Speed) //电机驱动函数
  171. {
  172.   if (m1Speed >= 0)
  173.   {
  174.     digitalWrite(PWMA2, HIGH);
  175.     analogWrite(PWMA1, 255 - m1Speed);
  176.     digitalWrite(PWMA4, HIGH);
  177.     analogWrite(PWMA3, 255 - m1Speed);
  178.    
  179.   }
  180.   else
  181.   {
  182.     digitalWrite(PWMA1, HIGH);
  183.     analogWrite(PWMA2, m1Speed + 255);
  184.      digitalWrite(PWMA3, HIGH);
  185.     analogWrite(PWMA4, m1Speed + 255);
  186.   }
  187. }

复制代码

所有资料51hei提供下载:
PS2运行程序.zip (320.1 KB, 下载次数: 38)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:364258 发表于 2019-7-17 23:39 来自手机 | 只看该作者
PS2X的库文件都没有,就一个程序有啥用,骗我5黑币
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表