Guest User

R/C Tank Wireless

a guest
Jul 14th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <PS2X_lib.h>  
  2. #include <Wire.h>
  3. #include <Adafruit_MotorShield.h>
  4. #include "utility/Adafruit_MS_PWMServoDriver.h"
  5.  
  6. #define PS2_DAT        9      
  7. #define PS2_CMD        11  
  8. #define PS2_SEL        10  
  9. #define PS2_CLK        12  
  10. #define pressures   false
  11. #define rumble      false
  12.  
  13. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  14. Adafruit_DCMotor *portSideMotor = AFMS.getMotor(3);
  15. Adafruit_DCMotor *starboardSideMotor = AFMS.getMotor(4);
  16.  
  17. PS2X ps2x;
  18. int error = 0;
  19. byte type = 0;
  20. byte vibrate = 0;
  21.  
  22. void setup(){
  23.  
  24.   Serial.begin(57600);
  25.  
  26.   delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it
  27.   pinMode(9, INPUT_PULLUP);
  28.   Serial.println("tank motors");
  29.   AFMS.begin();
  30.   error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  31.  
  32.   if(error == 0){
  33.     Serial.print("Found Controller, configured successful ");
  34.     Serial.print("pressures = ");
  35.     if (pressures)
  36.       Serial.println("true ");
  37.     else
  38.       Serial.println("false");
  39.     Serial.print("rumble = ");
  40.     if (rumble)
  41.       Serial.println("true)");
  42.     else
  43.       Serial.println("false");
  44.     Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  45.     Serial.println("holding L1 or R1 will print out the analog stick values.");
  46.     Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  47.   }  
  48.   else if(error == 1)
  49.     Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  50.    
  51.   else if(error == 2)
  52.     Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
  53.  
  54.   else if(error == 3)
  55.     Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  56.  
  57. //  Serial.print(ps2x.Analog(1), HEX);
  58.  
  59.   type = ps2x.readType();
  60.   switch(type) {
  61.     case 0:
  62.       Serial.print("Unknown Controller type found ");
  63.       break;
  64.     case 1:
  65.       Serial.print("DualShock Controller found ");
  66.       break;
  67.     case 2:
  68.       Serial.print("GuitarHero Controller found ");
  69.       break;
  70.     case 3:
  71.       Serial.print("Wireless Sony DualShock Controller found ");
  72.       break;
  73.    }
  74. }
  75.  
  76. void loop() {
  77.  
  78.   if(error == 1) //skip loop if no controller found
  79.     return;
  80.  
  81.   if(type == 2) //Guitar Hero Controller
  82.     return;
  83.    
  84.   else { //DualShock Controller
  85.     ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
  86.    
  87.     if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
  88.       Serial.println("Start is being held");
  89.     if(ps2x.Button(PSB_SELECT))
  90.       Serial.println("Select is being held");      
  91.  
  92.     if(ps2x.ButtonPressed(PSB_PAD_UP)) {      
  93.       Serial.println("TRIGGERED");
  94.       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  95.       portSideMotor->setSpeed(220);
  96.       starboardSideMotor->setSpeed(200);
  97.       portSideMotor->run(FORWARD);
  98.       starboardSideMotor->run(FORWARD);
  99.     }
  100.     if(ps2x.ButtonReleased(PSB_PAD_UP)){
  101.       portSideMotor->setSpeed(0);
  102.       starboardSideMotor->setSpeed(0);
  103.     }
  104.     if(ps2x.ButtonPressed(PSB_PAD_RIGHT)) {      
  105.       Serial.println("TRIGGERED");
  106.       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  107.       portSideMotor->setSpeed(105);
  108.       starboardSideMotor->setSpeed(100);
  109.       portSideMotor->run(FORWARD);
  110.       starboardSideMotor->run(BACKWARD);
  111.     }
  112.     if(ps2x.ButtonReleased(PSB_PAD_RIGHT)){
  113.       portSideMotor->setSpeed(0);
  114.       starboardSideMotor->setSpeed(0);
  115.     }
  116.     if(ps2x.ButtonPressed(PSB_PAD_LEFT)) {      
  117.       Serial.println("TRIGGERED");
  118.       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  119.       portSideMotor->setSpeed(105);
  120.       starboardSideMotor->setSpeed
  121.       (100);
  122.       portSideMotor->run(BACKWARD);
  123.       starboardSideMotor->run(FORWARD);
  124.     }
  125.     if(ps2x.ButtonReleased(PSB_PAD_LEFT)){
  126.       portSideMotor->setSpeed(0);
  127.       starboardSideMotor->setSpeed(0);
  128.     }
  129.     if(ps2x.ButtonPressed(PSB_PAD_DOWN)){
  130.       Serial.println("I DON'T FEEL SAFE");
  131.       Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  132.       portSideMotor->setSpeed(220);
  133.       starboardSideMotor->setSpeed(200);
  134.       portSideMotor->run(BACKWARD);
  135.       starboardSideMotor->run(BACKWARD);
  136.     }  
  137.     if(ps2x.ButtonReleased(PSB_PAD_DOWN)){
  138.       portSideMotor->setSpeed(0);
  139.       starboardSideMotor->setSpeed(0);
  140.     }
  141.     vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
  142.     if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)
  143.       if(ps2x.Button(PSB_L3))
  144.         Serial.println("L3 pressed");
  145.       if(ps2x.Button(PSB_R3))
  146.         Serial.println("R3 pressed");
  147.       if(ps2x.Button(PSB_L2))
  148.         Serial.println("L2 pressed");
  149.       if(ps2x.Button(PSB_R2))
  150.         Serial.println("R2 pressed");
  151.       if(ps2x.Button(PSB_TRIANGLE))
  152.         Serial.println("Triangle pressed");        
  153.     }
  154.  
  155.     if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
  156.       Serial.println("Circle just pressed");
  157.     if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
  158.       Serial.println("X just changed");
  159.     if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
  160.       Serial.println("Square just released");    
  161.  
  162.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
  163.       Serial.print("Stick Values:");
  164.       Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
  165.       Serial.print(",");
  166.       Serial.print(ps2x.Analog(PSS_LX), DEC);
  167.       Serial.print(",");
  168.       Serial.print(ps2x.Analog(PSS_RY), DEC);
  169.       Serial.print(",");
  170.       Serial.println(ps2x.Analog(PSS_RX), DEC);
  171.     }    
  172.   }
  173.   delay(50);  
  174. }
Add Comment
Please, Sign In to add comment