timor2542

[4 DOF Robot Arm Keyestudio][Lab 10][i-Duino UNO R3B] PSX2 Tester

Aug 2nd, 2021
1,252
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <PS2X_lib.h>  //for v1.6
  2.  
  3. PS2X ps2x; // create PS2 Controller Class
  4.  
  5. //right now, the library does NOT support hot pluggable controllers, meaning
  6. //you must always either restart your Arduino after you conect the controller,
  7. //or call config_gamepad(pins) again after connecting the controller.
  8. //int Left_motor_back=9; //Left motor back (IN1)
  9. // int Left_motor_go=5; //Left motor go forward (IN2)
  10. //
  11. // int Right_motor_go=6; // Right motor go forward (IN3)
  12. // int Right_motor_back=10; // Right motor back (IN4)
  13. //
  14. // int Right_motor_en=8; // Right motor forward (EN2)
  15. // int Left_motor_en=7; // right motor back (EN1)
  16.  
  17. int error = 0;
  18. byte type = 0;
  19. byte vibrate = 0;
  20.  
  21. void setup(){
  22. Serial.begin(115200);
  23.    //Initialize motor drive IO as output mode
  24. //  pinMode(Left_motor_go,OUTPUT); //
  25. //  pinMode(Left_motor_back,OUTPUT); //
  26. //  pinMode(Right_motor_go,OUTPUT);//
  27. //  pinMode(Right_motor_back,OUTPUT);//
  28. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  29.  
  30. error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  31.  
  32. if(error == 0){
  33.    Serial.println("Found Controller, configured successful");
  34.    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  35.   Serial.println("holding L1 or R1 will print out the analog stick values.");
  36.   Serial.println("Go to [url]www.billporter.info[/url] for updates and to report bugs.");
  37. }
  38.    
  39.   else if(error == 1)
  40.    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit [url]www.billporter.info[/url] for troubleshooting tips");
  41.    
  42.   else if(error == 2)
  43.    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url]www.billporter.info[/url] for troubleshooting tips");
  44.    
  45.   else if(error == 3)
  46.    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  47.    
  48.    //Serial.print(ps2x.Analog(1), HEX);
  49.    
  50.    type = ps2x.readType();
  51.      switch(type) {
  52.        case 0:
  53.         Serial.println("Unknown Controller type");
  54.        break;
  55.        case 1:
  56.         Serial.println("DualShock Controller Found");
  57.        break;
  58.        case 2:
  59.          Serial.println("GuitarHero Controller Found");
  60.        break;
  61.      }
  62.  
  63. }
  64.  
  65. void loop(){
  66.    /* You must Read Gamepad to get new values
  67.    Read GamePad and set vibration values
  68.    ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  69.    if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  70.    
  71.    you should call this at least once a second
  72.    */
  73.    
  74.    
  75.    
  76. if(error == 1) //skip loop if no controller found
  77.   return;
  78.  
  79. if(type == 2){ //Guitar Hero Controller
  80.    
  81.    ps2x.read_gamepad();          //read controller
  82.    
  83.    if(ps2x.ButtonPressed(GREEN_FRET))
  84.      Serial.println("Green Fret Pressed");
  85.    if(ps2x.ButtonPressed(RED_FRET))
  86.      Serial.println("Red Fret Pressed");
  87.    if(ps2x.ButtonPressed(YELLOW_FRET))
  88.      Serial.println("Yellow Fret Pressed");
  89.    if(ps2x.ButtonPressed(BLUE_FRET))
  90.      Serial.println("Blue Fret Pressed");
  91.    if(ps2x.ButtonPressed(ORANGE_FRET))
  92.      Serial.println("Orange Fret Pressed");
  93.      
  94.  
  95.     if(ps2x.ButtonPressed(STAR_POWER))
  96.      Serial.println("Star Power Command");
  97.    
  98.     if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
  99.      Serial.println("Up Strum");
  100.     if(ps2x.Button(DOWN_STRUM))
  101.      Serial.println("DOWN Strum");
  102.  
  103.  
  104.     if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
  105.          Serial.println("Start is being held");
  106.     if(ps2x.Button(PSB_SELECT))
  107.          Serial.println("Select is being held");
  108.  
  109.    
  110.     if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
  111.     {
  112.         Serial.print("Wammy Bar Position:");
  113.         Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  114.     }
  115. }
  116.  
  117. else { //DualShock Controller
  118.  
  119.    ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
  120.    
  121.    if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
  122.          Serial.println("Start is being held");
  123.     if(ps2x.Button(PSB_SELECT))
  124.          Serial.println("Select is being held");
  125.          
  126.          
  127.     if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
  128.        Serial.print("Up held this hard: ");
  129.        Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  130.       }
  131.       if(ps2x.Button(PSB_PAD_RIGHT)){
  132.        Serial.print("Right held this hard: ");
  133.         Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
  134.       }
  135.       if(ps2x.Button(PSB_PAD_LEFT)){
  136.        Serial.print("LEFT held this hard: ");
  137.         Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
  138.       }
  139.       if(ps2x.Button(PSB_PAD_DOWN)){
  140.        Serial.print("DOWN held this hard: ");
  141.      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  142.       }  
  143.  
  144.    
  145.       vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on
  146.                                               //how hard you press the blue (X) button    
  147.    
  148.     if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)
  149.     {
  150.      
  151.        
  152.          
  153.         if(ps2x.Button(PSB_L3))
  154.          Serial.println("L3 pressed");
  155.         if(ps2x.Button(PSB_R3))
  156.          Serial.println("R3 pressed");
  157.         if(ps2x.Button(PSB_L2))
  158.          Serial.println("L2 pressed");
  159.         if(ps2x.Button(PSB_R2))
  160.          Serial.println("R2 pressed");
  161.         if(ps2x.Button(PSB_GREEN))
  162.          Serial.println("Triangle pressed");
  163.          
  164.     }  
  165.          
  166.    
  167.     if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed
  168.          Serial.println("Circle just pressed");
  169.          
  170.     if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released
  171.          Serial.println("Square just released");    
  172.    
  173.     if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released
  174.          Serial.println("X just changed");    
  175.    
  176.    
  177.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
  178.     {
  179.         Serial.print("Stick Values:");
  180.         Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
  181.         Serial.print(",");
  182.         Serial.print(ps2x.Analog(PSS_LX), DEC);
  183.         Serial.print(",");
  184.         Serial.print(ps2x.Analog(PSS_RY), DEC);
  185.         Serial.print(",");
  186.         Serial.println(ps2x.Analog(PSS_RX), DEC);
  187.     }
  188.    
  189.    
  190. }
  191. delay(50);
  192.      
  193. }
  194.  
RAW Paste Data